473,406 Members | 2,894 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,406 software developers and data experts.

Appending XML to existing XmlDocument

I have a database class that maintains data about customers i my system.
The basic XML for this looks like:

<Chunk>
<Vendor>
<Database/>
</Vendor>
</Chunk>

When a user is to be registrered in the system, XML like this is created
in a seperate XML-class (Xml):

<User>
<UserName>stocholm</UserName>
<NumberOfItemsBought>0</NumberOfItemsBought>
<SignOfLastImage>A31A20AB338B6F2FD772ECFA0</SignOfLastImage>
<n>DB1A46496C83DFFC0CA2BA91585AA25E90195B77DB5997D A3D</n>
<e>AAB7E2A0F</e>
</User>

This piece of XML should be appended as a child of the element "User"
such that the XML will look like this:

<Chunk>
<Vendor>
<Database>
<User>
<UserName>stocholm</UserName>
<NumberOfItemsBought>0</NumberOfItemsBought>
<SignOfLastImage>A31A20AB338B6F2FE</SignOfLastImage>
<n>B1A46496C83DFFC0CA2BA91585AA25E90195B77DB5997 </n>
<e>AAB7E2A0F</e>
</User>
</Database>
</Vendor>
</Chunk>

But how do I insert the new piece of XML via DOM? My base xml is loaded
in an XmlDocument object, and the new XML is created like this in my Xml
class:

public static XmlNode CreateXmlForDatabase(
string userName,string n,string e,string signOfLastImage
)
{
XmlDocument xDoc = new XmlDocument();
XmlNode node = xDoc.CreateNode(XmlNodeType.Element,"User",null);
XmlElement elem = xDoc.CreateElement(null,"UserName",null);
elem.InnerText = userName;
node.AppendChild(elem);
elem = xDoc.CreateElement(null,"NumberOfItemsBought",null );
elem.InnerText = "0";
node.AppendChild(elem);
elem = xDoc.CreateElement(null,"SignOfLastImage",null);
elem.InnerText = signOfLastImage;
node.AppendChild(elem);
elem = xDoc.CreateElement(null,"n",null);
elem.InnerText = n;
node.AppendChild(elem);
elem = xDoc.CreateElement(null,"e",null);
elem.InnerText = e;
node.AppendChild(elem);
return node;
}

If I try with this in my database class:

XmlNode node = Xml.CreateXmlForDatabase(username,n,e,SignOfLastIm age);
XmlNode newUser = _xDoc.SelectSingleNode("/Chunk/Vendor/Database");
newUser.AppendChild(node);

I get the error

"The node to be inserted is from a different document context."

Why is that?

The base XML in my database class is initialized as

static XmlDocument _xDoc = new XmlDocument();
_xDoc.LoadXml("<Chunk><Vendor><Database/></Vendor></Chunk>");

I hope you can help me out on this one,

Thanks, :o)

--
Jesper Stocholm
http://stocholm.dk
Give a man a fish and he will have food for a day,
give a man an elephant, and he will have food for a week.
Nov 15 '05 #1
4 18151
Jesper Stocholm <j@stocholm.invalid> wrote:

<snip>
If I try with this in my database class:

XmlNode node = Xml.CreateXmlForDatabase(username,n,e,SignOfLastIm age);
XmlNode newUser = _xDoc.SelectSingleNode("/Chunk/Vendor/Database");
newUser.AppendChild(node);

I get the error

"The node to be inserted is from a different document context."


The easiest way to get away from this is to pass the XmlDocument you'll
be including it in to CreateXmlForDatabase rather than creating an
XmlDocument within the method. However, you could also use the
XmlDocument.ImportNode method - see the details within MSDN for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Jon Skeet [C# MVP] wrote :

Hi Jon, thanks for your prompt reply,
Jesper Stocholm <j@stocholm.invalid> wrote:
If I try with this in my database class:

XmlNode node = Xml.CreateXmlForDatabase(username,n,e,SignOfLastIm age);
XmlNode newUser = _xDoc.SelectSingleNode("/Chunk/Vendor/Database");
newUser.AppendChild(node);

I get the error

"The node to be inserted is from a different document context."


The easiest way to get away from this is to pass the XmlDocument you'll
be including it in to CreateXmlForDatabase rather than creating an
XmlDocument within the method. However, you could also use the
XmlDocument.ImportNode method - see the details within MSDN for more
information.


I have tried with ImortNode-method, and it nicely imports the data from
the new XmlDocument ... only it is at the wrong place.

My code is this:

XmlDocument newUser = new XmlDocument();
newUser = Xml.CreateXmlForDatabase(username,n,e,SignOfLastIm age);
XmlNode nodeUser = _xDoc.ImportNode(newUser.SelectSingleNode
("User"),true);
_xDoc.DocumentElement.AppendChild(nodeUser);

The Xml should look like

<Chunk>
<Vendor>
<Database>
<User>
<UserName>stocholm</UserName>
...
</User>
</Database>
</Vendor>
</Chunk>

But it is now

<Chunk>
<Vendor>
<Database/>
</Vendor>
<User>
<UserName>stocholm</UserName>
...
</User>
</Chunk>

How can I make it import the XML-chunk as a child of the Database-
element?

--
Jesper Stocholm
http://stocholm.dk
Give a man a fish and he will have food for a day,
give a man an elephant, and he will have food for a week.
Nov 15 '05 #3
Jesper Stocholm <j@stocholm.invalid> wrote:
My code is this:

XmlDocument newUser = new XmlDocument();
newUser = Xml.CreateXmlForDatabase(username,n,e,SignOfLastIm age);
XmlNode nodeUser = _xDoc.ImportNode(newUser.SelectSingleNode
("User"),true);
_xDoc.DocumentElement.AppendChild(nodeUser);
<snip>
How can I make it import the XML-chunk as a child of the Database-
element?


Just append it as a child of the appropriate element. You're calling
DocumentElement.AppendChild, so it's appending it to the
DocumentElement (which is <Chunk>). There are any number of ways of
getting to the right node (e.g. XPath, straight DOM manipulation etc).
Once you've got the right node, just call AppendChild on that, instead
of the DocumentElement.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Jesper Stocholm wrote :
Jon Skeet [C# MVP] wrote :

The easiest way to get away from this is to pass the XmlDocument
you'll be including it in to CreateXmlForDatabase rather than
creating an XmlDocument within the method. However, you could also
use the XmlDocument.ImportNode method - see the details within MSDN
for more information.


I have tried with ImortNode-method, and it nicely imports the data
from the new XmlDocument ... only it is at the wrong place.

How can I make it import the XML-chunk as a child of the Database-
element?


I found the answer myself. This code does the trick:

XmlDocument newUser = new XmlDocument();
newUser = Xml.CreateXmlForDatabase(username,n,e,SignOfLastIm age);
XmlNode nodeDatabase = _xDoc.SelectSingleNode("/Chunk/Vendor/Database");
XmlNode nodeUser = _xDoc.ImportNode(newUser.SelectSingleNode("User"), true);
nodeDatabase.AppendChild(nodeUser);

:o)

--
Jesper Stocholm
http://stocholm.dk
Give a man a fish and he will have food for a day,
give a man an elephant, and he will have food for a week.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kleist | last post by:
Hello, I use DOM XML PHP functions to build a document. Is it possible to insert a new node as sibling right after the selected by xpath node? There is a function insert_before() but it seems...
3
by: ArmsTom | last post by:
I know this question has been asked several times over. For some reason I cannot get it to sink in. I would like to create a xml document fragment (<--I think that's what I want, tell me if I'm...
1
by: Novice | last post by:
Hey all, I've read a few articles about speed and XML processing - so I just want to make sure that I'm using the right strategy for what I want to achieve. I have an XML file that I'm appending...
8
by: yinjennytam | last post by:
Hi all, I'm new to .NET and XML and I have a question. Given an XML file, I want to navigate its content and look for one or two particular elements to get their values. At this point, it...
2
by: Cat | last post by:
How do you go about appending data from a dataset to an existing xml file? I know you can use WriteXML but this writes over any data already existing in the specified file.. Cat
2
by: feng | last post by:
I think I didn't phrase my quetion clear enough in the last post. Here is what I need: In my VB.Net code, I already have a XML created in XMLDocument formate. I can also convert it into a string...
13
by: sherifffruitfly | last post by:
Hi all, I'm trying to distill all of the info from google searches into what I need, with partial success. In truth, the whole xmlNode, Document, Element, etc group of classes & methods is going...
2
by: Phil Galey | last post by:
I'm using the followg code to add the attribute overwrite='true" to a select list of XML tags in an XML document. The document is loaded from a file and just the tags with names matching what's in...
3
by: Stephen Ward | last post by:
I have a simple little project open a xml file change a few nodes save the file, no big deal. The problem is that the doctype is getting modified when I save the file. So it looks like this:...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.