472,352 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 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 17984
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?...
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...
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...
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...
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...
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...
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,...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.