473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

add nodes to existing xml document

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
1 36774
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tom | last post by:
Hi, this snippet of code gives an error in Firefox but works just fine in IE. <html> <head> <script type="text/javascript"> function start(){ statusarray = new Array();...
6
by: Roee Bachar | last post by:
I've site that is divided into two frames Frame 1 Frame 2 ??????????????????????? ? ? ? ? ? ...
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
0
by: Anton | last post by:
Hi All, I have been trying to mail merge to a document that was created previously via automation. Scenario: Use a .dot template - connect using sql statement to filter records - save...
6
by: J Williams | last post by:
I'm using axWebBrowser control and HTML DOM in a VB .NET Windows application to create a new HTML document by cloning nodes. The function below is called from the axWebBrowser1_DocumentComplete...
1
by: Kürşat | last post by:
Hi all, I want to write a class which define some frequent XML operations. For example a method named "getElementByAttribute (string attrName, string attrVal)" should return an XmlElement...
1
by: Todd Beaulieu | last post by:
I want to build an XML document from a number of sub documents. As I call objects in my data structure, each would return their own ToXml(), which I want to append to the main document that I'm...
3
by: Steven.Xu | last post by:
hi everybody, i am useing some classes which in System.Xml to deal with xml document. what can i do if the document include "<" or ">"? Thanks.
2
by: orange123 | last post by:
Hi Can someone help me please? (I am doing all this code in VB.NET) I have been trying to convert an existing document (word, excel, pdf, powerpoint, etc) to an image field) in my sql server...
1
by: eggie5 | last post by:
All I want to do is select all the nodes under the docment root. e.g: <?xml version="1.0" encoding="UTF-8"?> <Categories> <Category> <content_provider_id type="integer">1</content_provider_id>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.