Abdu_Allah
asked on
Append node to XML file?
How can I add the following node:
<user>
<name> xxx </name>
<id>1</id>
<mark A="10" B="20" />
</user>
To the following XML file using XMLDocument or XMLwriterText:
<users>
<user>
<name> yyy</name>
<id>1</id>
<mark A="11" B="12" />
</user>
<user>
<name> zzz</name>
<id>2</id>
<mark A="34" B="50" />
</user>
</users>
<user>
<name> xxx </name>
<id>1</id>
<mark A="10" B="20" />
</user>
To the following XML file using XMLDocument or XMLwriterText:
<users>
<user>
<name> yyy</name>
<id>1</id>
<mark A="11" B="12" />
</user>
<user>
<name> zzz</name>
<id>2</id>
<mark A="34" B="50" />
</user>
</users>
ASKER
Whay I got the following error:
System.InvalidOperationExc eption: The current node cannot contain other nodes
Source Error:
Line 126: doc.FirstChild.AppendChild (user)
System.InvalidOperationExc
Source Error:
Line 126: doc.FirstChild.AppendChild
What does the XML you are appending to look like in full ? Is there a Schema or DTD associated with it ?
ASKER
No schema no DTD
What does your full XML look like ?
ASKER
Here is the full file:
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<name> yyy</name>
<id>1</id>
<mark A="11" B="12" />
</user>
<user>
<name> zzz</name>
<id>2</id>
<mark A="34" B="50" />
</user>
</users>
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<name> yyy</name>
<id>1</id>
<mark A="11" B="12" />
</user>
<user>
<name> zzz</name>
<id>2</id>
<mark A="34" B="50" />
</user>
</users>
Try it with just the snippet you posted before and see if that works.
ASKER
Same error.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks
XmlDocument doc = new XmlDocument();
doc.Load("C:\\test.xml");
XmlNode user = doc.CreateElement("user");
user.AppendChild(doc.Creat
user.AppendChild(doc.Creat
XmlNode mark = doc.CreateElement("mark");
mark.Attributes.SetNamedIt
mark.Attributes.SetNamedIt
user.AppendChild(mark);
doc.FirstChild.AppendChild
doc.Save("C:\\test2.xml");