By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,416 Members | 1,396 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,416 IT Pros & Developers. It's quick & easy.

add nodes to existing xml document

P: n/a
I have an existing xml file taht looks like this.
<?xml version="1.0" encoding="utf-8" ?>
<Aircraft>
<WCOJOItemType type="AircraftType" >
<WCJOItemCode>03</WCJOItemCode>
<WCJOItemDescription>B737-700</WCJOItemDescription>
</WCOJOItemType>
</Aircraft>

I want to be able to add nodes to the doc using C#. I have tried the
following code and it fails at line 60. I get the following error.
"The node to be inserted is from a different document context."

If I change line 60 to
doc.InsertAfter((XmlNode)newItemType,root);
then I get the following error:
"The reference node is not a child of this node."

Please help.
1 XmlDocument doc = new XmlDocument();
2 XmlDocument newdoc = new XmlDocument();
3
4
5 string ln;
6 bool isFile;
7
8 //check to see that the file is there
9 isFile = File.Exists(path);
10
11 //place the xml into a stream for reading
12 StreamReader fs = new StreamReader(path);
13
14
15 StringBuilder inputdoc = new StringBuilder();
16 //read the xml into a single string
17 while((ln = fs.ReadLine()) != null)
18 {
19 inputdoc.Append(ln.Trim());
20 }
21 //place the string into something that an XmlTextReader can read
22 StringReader file = new StringReader(inputdoc.ToString());
23
24 XmlTextReader reader = new XmlTextReader(file);
25 fs.Close();
26
27 //load an XmlDocument from the string of xml in the file
28 newdoc.Load(reader);
29
30 reader.Close();
31
32
33 //create the new elements that will be added to the doc
34 XmlElement newItemType = doc.CreateElement("WCJOItemType");
35 XmlElement newItemCode = doc.CreateElement("WCJOItemCode");
36 XmlElement newItemDesc = doc.CreateElement("WCJOItemDescription");
37
38 //append the new elements to a XmlDocument
39 doc.AppendChild(newItemType);
40 newItemType.AppendChild(newItemCode);
41 newItemType.AppendChild(newItemDesc);
42
43 //add an attribute to the WCOJOItemType element
44 XmlAttribute typeAttribute = doc.CreateAttribute("type");
45 typeAttribute.Value = "AircrafType";
46
47 newItemType.SetAttributeNode(typeAttribute);
48
49 //set the innertext on the child nodes from the value of some
object
50 newItemCode.InnerText = hlpItem.WCJOItemCode.ToString();
51 newItemDesc.InnerText = hlpItem.WCJOItemDescription.ToString();
52
53 //get the <aircraft> node
54 XmlNode root = newdoc.FirstChild;
55
56 //verify that the <Aircraft> is the node that has been obtained
57 string test = root.Name;
58
59 //insert the new nodes
60 newdoc.InsertAfter((XmlNode)newItemType,root);
61
62
63
64 FileStream docout = new
FileStream(path,FileMode.Append,FileAccess.Write,F ileShare.ReadWrite);
65 doc.Save(docout);
66
67 docout.Close();
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
wj****@coair.com (Brent Jenny) wrote in message news:<8f**************************@posting.google. com>...

--SNIP--

Try this instead for line 60;

newdoc.InsertAfter(newdoc.ImportNode((XmlNode)newI temType,true),root);

This way you are copying the node, but setting this temp node's
parrent doc to the doc you are inserting into.

Hope this helps

Andy Renk

Junker_Mail(BLABLA)@yahoo.com Take out "(BLABLA)" to email
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.