|
-
Aug 2nd, 2004, 12:46 AM
#1
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:
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.
Last edited by mendhak; Aug 2nd, 2004 at 02:14 AM.
-
Aug 2nd, 2004, 02:14 AM
#2
Here's what was missing:
VB Code:
XMLd.Save(Server.MapPath("family.xml"))
Right after the last statement.
Don't I just hate it when I solve it myself? Every time.
-
Aug 2nd, 2004, 02:27 AM
#3
Can someone verify if this is the way "things are done"?
VB Code:
Dim XMLd As New XmlDocument
Dim XMLN As XmlNode
'and xmldocument, to represent the instance of the XML file we're dealing with.
XMLd.Load(Server.MapPath("family.xml"))
'An XML Element <name>
Dim xmlEl As XmlElement = XMLd.CreateElement("name")
'It can have an attribute gender=...>
Dim xmlAttr As XmlAttribute = XMLd.CreateAttribute("gender")
'gender="male">
xmlAttr.Value = "male"
' now we have <name gender="male">
xmlEl.Attributes.Append(xmlAttr)
'one more attribute, for old times sake...
xmlAttr = XMLd.CreateAttribute("moron")
xmlAttr.Value = "True"
'now, we add the attribute to the element. Again.
xmlEl.Attributes.Append(xmlAttr)
'What goes INside the element we just made.
xmlEl.InnerXml = "<firstname></firstname><lastname></lastname>"
'give it some values
xmlEl.Item("firstname").InnerText = "Alfred E"
xmlEl.Item("lastname").InnerText = "Neumann"
'append the whole element to the whole damn document.
XMLd.DocumentElement.AppendChild(xmlEl)
'save the document now!
XMLd.Save(Server.MapPath("family.xml"))
-
Aug 2nd, 2004, 10:07 AM
#4
New Member
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"))
_______________________________________________
-
Aug 3rd, 2004, 12:09 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|