Results 1 to 5 of 5

Thread: XML - Appending [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    XML - Appending [Resolved]

    I have an XML file with the following structure:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <family>
      <name gender="Male">
        <firstname>Tom</firstname>
        <lastname>Smith</lastname>
      </name>
      <name gender="Female">
        <firstname>Dale</firstname>
        <lastname>Doorknob</lastname>
      </name>
    </family>
    Now I'd like to append a node to this through code only. Here is what I have so far:

    VB Code:
    1. Dim XMLd As New XmlDocument
    2.        
    3.  
    4.  
    5.         XMLd.Load(Server.MapPath("family.xml")) 'this is fine, no problem with mapping!
    6.        
    7.  
    8.         Dim xmlEl As XmlElement = XMLd.CreateElement("name")
    9.         Dim xmlAttr As XmlAttribute = XMLd.CreateAttribute("gender")
    10.         xmlAttr.Value = "male"
    11.         xmlEl.Attributes.Append(xmlAttr)
    12.  
    13.         xmlEl.InnerXml = "<firstname></firstname><lastname></lastname>"
    14.  
    15.         Dim txtNode As XmlText = XMLd.CreateTextNode("Alfed E")
    16.  
    17.         xmlEl.FirstChild.AppendChild(txtNode)
    18.         xmlEl.Item("lastname").InnerText = "Neumann"
    19.  
    20.         XMLd.DocumentElement.AppendChild(xmlEl)


    When I run the code, very simply, this is what happens:

    Nothing.

    Absolutely nothing. Zilch. Nada. Zero! (Yes, with the exclamation)

    So what am I missing? Even an existing example similar to this would be helpful.
    Last edited by mendhak; Aug 2nd, 2004 at 02:14 AM.

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Here's what was missing:

    VB Code:
    1. XMLd.Save(Server.MapPath("family.xml"))

    Right after the last statement.


    Don't I just hate it when I solve it myself? Every time.

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Can someone verify if this is the way "things are done"?

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

  4. #4
    New Member
    Join Date
    Jul 2004
    Location
    ga
    Posts
    2
    There's more than one way to skin a cat, but I would have done it something like this...but yeah, yours works too...


    _______________________________________________ Dim XMLd As New XmlDocument

    XMLd.Load(Server.MapPath("family.xml"))

    Dim xmlEl As XmlElement = XMLd.CreateElement("name")
    xmlEl.setAttribute "gender", "male"
    xmlEl.setAttribute "moron", "True"

    dim fName as xmlElement = xmlEl.appendChild(XMLd.createElement("firstname"))
    dim lName as xmlElement = xmlEl.appendChild(XMLd.createElement("lastname"))

    fName.innerText = "Alfred E"
    lName.innerText = "Neumann"

    XMLd.DocumentElement.AppendChild(xmlEl)

    XMLd.Save(Server.MapPath("family.xml"))
    _______________________________________________

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Excellent, I feel I was close enough! Thanks.

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