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>stoch olm</UserName>
<NumberOfItemsB ought>0</NumberOfItemsBo ught>
<SignOfLastImag e>A31A20AB338B6 F2FD772ECFA0</SignOfLastImage >
<n>DB1A46496C83 DFFC0CA2BA91585 AA25E90195B77DB 5997DA3D</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>stoch olm</UserName>
<NumberOfItemsB ought>0</NumberOfItemsBo ught>
<SignOfLastImag e>A31A20AB338B6 F2FE</SignOfLastImage >
<n>B1A46496C83D FFC0CA2BA91585A A25E90195B77DB5 997</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 CreateXmlForDat abase(
string userName,string n,string e,string signOfLastImage
)
{
XmlDocument xDoc = new XmlDocument();
XmlNode node = xDoc.CreateNode (XmlNodeType.El ement,"User",nu ll);
XmlElement elem = xDoc.CreateElem ent(null,"UserN ame",null);
elem.InnerText = userName;
node.AppendChil d(elem);
elem = xDoc.CreateElem ent(null,"Numbe rOfItemsBought" ,null);
elem.InnerText = "0";
node.AppendChil d(elem);
elem = xDoc.CreateElem ent(null,"SignO fLastImage",nul l);
elem.InnerText = signOfLastImage ;
node.AppendChil d(elem);
elem = xDoc.CreateElem ent(null,"n",nu ll);
elem.InnerText = n;
node.AppendChil d(elem);
elem = xDoc.CreateElem ent(null,"e",nu ll);
elem.InnerText = e;
node.AppendChil d(elem);
return node;
}
If I try with this in my database class:
XmlNode node = Xml.CreateXmlFo rDatabase(usern ame,n,e,SignOfL astImage);
XmlNode newUser = _xDoc.SelectSin gleNode("/Chunk/Vendor/Database");
newUser.AppendC hild(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.