在现有XML文档中附加新节点

时间:2014-02-03 17:58:37

标签: asp.net xml

我想在现有的XML中添加新节点。我的XML结构如下: -

<NewDataSet>
  <Table>
    <HotelName>A</HotelName>
    <Rating>5*</Rating>
    <Hzone>Central </Hzone>
    <HBeds>B</HBeds>
    <Address>Lodhi Road</Address>
    <Soh>-</Soh>
    <Recommended>0</Recommended>
    <DetailStr>-</DetailStr>
    <Block>N</Block>
  </Table>
  <Table>
    <HotelName>B</HotelName>
    <Rating>5*</Rating>
    <Hzone>Central </Hzone>
    <HBeds>A</HBeds>
    <Address>Lodhi Road</Address>
    <Soh>Bh</Soh>
    <Recommended>0</Recommended>
    <DetailStr>-</DetailStr>
    <Block>N</Block>
  </Table>
<NewDataSet>

我想在Tag之后添加节点,即我希望我的XML为

<NewDataSet>
  <Table>
    <HotelName>A</HotelName>
    <Rating>5*</Rating>
    <Hzone>Central </Hzone>
    <Address>Lodhi Road</Address>
    <HBeds>B</HBeds>
    <Soh>-</Soh>
    <Recommended>0</Recommended>
    <DetailStr>-</DetailStr>
    <Block>N</Block>
    <HId>1</HId>
  </Table>
  <Table>
    <HotelName>B</HotelName>
    <Rating>5*</Rating>
    <Hzone>Central </Hzone>
    <Address>Lodhi Road</Address>
    <HBeds>A</HBeds>
    <Soh>Bh</Soh>
    <Recommended>0</Recommended>
    <DetailStr>-</DetailStr>
    <Block>N</Block>
    <HId>2</HId>
  </Table>
<NewDataSet>

我使用以下代码从数据库中获取HId的值。

Dim XmlNodeListPackes As XmlNodeList
XmlNodeListPackes = xDoc.SelectNodes("/NewDataSet/Table") 
For Each xNode As XmlNode In XmlNodeListPackes

                Dim strHotelBeds As String = xNode.SelectSingleNode("Hbeds").InnerText
                Dim City As String = txtxmlfile.Text
                HId = DAL.GetHotelBedId(strHotelBeds, City)

            Next

我想在XML中插入这个HId。请帮我解释一下代码。

由于

2 个答案:

答案 0 :(得分:1)

获得HId值后立即执行此操作:

Dim xHIdNode = xNode.SelectSingleNode("HId")

If xHIdNode Is Nothing
    xHIdNode = xDoc.CreateNode(XmlNodeType.Element, "HId", Nothing)
    xNode.AppendChild(xNewNode)
End If

xHIdNode.Value = HId

那应该做的。

答案 1 :(得分:1)

你应该注意的几件事情:

  1. 缺少Xml结束标记/

  2. Xml区分大小写。 <HBeds><Hbeds>不同!

  3. 以下是我们如何按照问题中的要求插入<HId>个节点的示例。

    在我的项目中,我有xml文件“MyXml.xml”:

    <?xml version="1.0" encoding="utf-8" ?>
    <NewDataSet>
      <Table>
        <HotelName>A</HotelName>
        <Rating>5*</Rating>
        <Hzone>Central </Hzone>
        <HBeds>B</HBeds>
        <Address>Lodhi Road</Address>
        <Soh>-</Soh>
        <Recommended>0</Recommended>
        <DetailStr>-</DetailStr>
        <Block>N</Block>
      </Table>
      <Table>
        <HotelName>B</HotelName>
        <Rating>5*</Rating>
        <Hzone>Central </Hzone>
        <HBeds>A</HBeds>
        <Address>Lodhi Road</Address>
        <Soh>Bh</Soh>
        <Recommended>0</Recommended>
        <DetailStr>-</DetailStr>
        <Block>N</Block>
      </Table>
    </NewDataSet>
    

    我有一个带有此标记的测试网页 WebForm1.aspx

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="VBXmlTest.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtxmlfile" runat="server">Vancouver</asp:TextBox>
        </div>
        </form>
    </body>
    </html>
    

    在后面的代码中,Page_Load我正在调用ProcessXml函数,我的 WebForm1.aspx.vb 是:

    Imports System.Xml
    
    Public Class WebForm1
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ProcessXml()
        End Sub
    
        Private Sub ProcessXml()
            Dim xDoc As New XmlDocument
            Dim filePath As String = Server.MapPath("~/MyXml.xml")
            xDoc.Load(filePath)
            Dim XmlNodeListPackes As XmlNodeList
            XmlNodeListPackes = xDoc.SelectNodes("/NewDataSet/Table")
            Dim Hid As Integer
            For Each xNode As XmlNode In XmlNodeListPackes
    
                Dim strHotelBeds As String = xNode.SelectSingleNode("HBeds").InnerText
                Dim City As String = txtxmlfile.Text
                Hid = DAL.GetHotelBedId(strHotelBeds, City)
                Dim hidNode As XmlNode = xNode.SelectSingleNode("HId")
                If hidNode IsNot Nothing Then
                    hidNode.InnerText = Hid
                Else
                    hidNode = xDoc.CreateNode(XmlNodeType.Element, "HId", "")
                    hidNode.InnerText = Hid
                    xNode.AppendChild(hidNode)
                End If
            Next
            xDoc.Save(filePath)
        End Sub
    
    End Class
    

    为了测试,我已将DAL替换为模块,仅用于测试:

    Public Module DAL
        Public Function GetHotelBedId(ByVal strHotelBeds As String, ByVal City As String) As Integer
            Return 1
        End Function
    End Module
    

    这是输出xml,无论我运行多少次:

    <?xml version="1.0" encoding="utf-8"?>
    <NewDataSet>
      <Table>
        <HotelName>A</HotelName>
        <Rating>5*</Rating>
        <Hzone>Central </Hzone>
        <HBeds>B</HBeds>
        <Address>Lodhi Road</Address>
        <Soh>-</Soh>
        <Recommended>0</Recommended>
        <DetailStr>-</DetailStr>
        <Block>N</Block>
        <HId>1</HId>
      </Table>
      <Table>
        <HotelName>B</HotelName>
        <Rating>5*</Rating>
        <Hzone>Central </Hzone>
        <HBeds>A</HBeds>
        <Address>Lodhi Road</Address>
        <Soh>Bh</Soh>
        <Recommended>0</Recommended>
        <DetailStr>-</DetailStr>
        <Block>N</Block>
        <HId>1</HId>
      </Table>
    </NewDataSet>