Results 1 to 2 of 2

Thread: VB.NET: Append string to Xml file

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Append string to Xml file

    Hi

    Say i receive one string full of Xml tags ...

    Example: "<Person>John</Person><Item>Pen</Item><Amount>4</Amount>"


    How to save and append these tags to my xml files..

    They told me to use appendChild method.. maybe i am not quite sure with string....... i save the string two times instead..
    I am using VB.NET....
    Code:
     Dim myDoc As New Xml.XmlDocument
     Dim myXml As String
      myXml = "<Person>John</Person><Item>Pen</Item> _  
     <Amount>4</Amount>
    
      myDoc.LoadXml(myXml)
    
      Dim root As XmlNode = myDoc.DocumentElement
    
      Dim person As XmlElement = myDoc.CreateElement("Person")
      person.InnerText = John
    
      Dim itemy As XmlElement = myDoc.CreateElement("Item")
      item.InnerText = pen
    
      Dim amount As XmlElement = myDoc.CreateElement("Amount")
      amount.InnerText = 4
    
      root.AppendChild(person)
      root.AppendChild(item) 
      root.AppendChild(amount)
      myDoc.Save("1.xml")
    How to solve this so that it will append to the xml whenever i receive a string...

    Thanks.
    Last edited by toytoy; Dec 3rd, 2004 at 08:15 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Are you adding a new node?

    See if this helps:

    VB Code:
    1. Sub NewNodeInXMLFile(ByVal strPath As String, ByVal strGender As String, ByVal strFirstName As String, ByVal strLastName As String)
    2.         Dim XMLd As New XmlDocument
    3.         Dim XMLN As XmlNode
    4.  
    5.  
    6.         'and xmldocument, to represent the instance of the XML file we're dealing with.
    7.         XMLd.Load(strPath)
    8.  
    9.         'An XML Element <name>
    10.         Dim xmlEl As XmlElement = XMLd.CreateElement("name")
    11.         'It can have an attribute gender=...>
    12.         Dim xmlAttr As XmlAttribute = XMLd.CreateAttribute("gender")
    13.         'gender="male">
    14.         xmlAttr.Value = strGender
    15.  
    16.  
    17.  
    18.         ' now we have <name gender="male">
    19.         xmlEl.Attributes.Append(xmlAttr)
    20.         'one more attribute, for old times sake...
    21.         xmlAttr = XMLd.CreateAttribute("moron")
    22.         xmlAttr.Value = "True"
    23.         'now, we add the attribute to the element.  Again.
    24.         xmlEl.Attributes.Append(xmlAttr)
    25.  
    26.         'What goes INside the element we just made.
    27.         xmlEl.InnerXml = "<firstname></firstname><lastname></lastname>"
    28.  
    29.  
    30.  
    31.  
    32.         'give it some values
    33.         xmlEl.Item("firstname").InnerText = strFirstName
    34.         xmlEl.Item("lastname").InnerText = strLastName
    35.  
    36.  
    37.         'append the whole element to the whole damn document.
    38.         XMLd.DocumentElement.AppendChild(xmlEl)
    39.         'save the document now!
    40.         XMLd.Save(Server.MapPath("family.xml"))
    41.  
    42.  
    43.         'navigation
    44.         'delete node
    45.  
    46. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width