I have an XML file with the following structure:
Now I'd like to append a node to this through code only. Here is what I have so far: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>
VB Code:
Dim XMLd As New XmlDocument XMLd.Load(Server.MapPath("family.xml")) 'this is fine, no problem with mapping! Dim xmlEl As XmlElement = XMLd.CreateElement("name") Dim xmlAttr As XmlAttribute = XMLd.CreateAttribute("gender") xmlAttr.Value = "male" xmlEl.Attributes.Append(xmlAttr) xmlEl.InnerXml = "<firstname></firstname><lastname></lastname>" Dim txtNode As XmlText = XMLd.CreateTextNode("Alfed E") xmlEl.FirstChild.AppendChild(txtNode) xmlEl.Item("lastname").InnerText = "Neumann" 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.




Reply With Quote