locked
Append a New Node in already existing XML Document RRS feed

  • Question

  • User1216489373 posted

    Hi Experts,

    I want to append new node in already existing XML. My XML structure is as follows:-

    <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>

    I want to add <HId> node after <Block> Tag i.e. I want my XML as

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

    I am getting the value of HId from Database using following code.

    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

    I want to insert this HId in XML. Please help me with the code.

    Thanks,

    Rahul

    Monday, February 3, 2014 12:46 PM

Answers

  • User-933407369 posted

    hi,

    According to your description, i would suggest you try the code below:

    Dim xDoc As New XmlDocument
            Dim filePath As String = Server.MapPath("~/MyXml.xml")
            xDoc.Load(filePath)
            Dim XmlNodeListPackes As XmlNodeList
            'select your code which you want to append
            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)
    

    please refer to the link below:

    Add Node existing XML file

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/e3f3c6b1-43ee-46d7-bc09-edb8dcedb8d1/add-node-existing-xml-file?forum=csharpgeneral

    Hope it helps you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 4, 2014 8:12 AM