same XmlDocument across different classes

M

Mungo Jerrie

Hi there :)

I have some problems with the same instance of my xmldocument across 3
different classes. See code below:

class one {

protected static XmlDocument doc = new XmlDocument();

public static XmlDocument getXmlDocument(){
XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDecl = doc.CreateXmlDeclaration("1.0",
"iso-8859-1", null); //"iso-8859-1"
doc.InsertBefore(xmlDecl, doc.DocumentElement);
return doc;
}
}

class two{

private static XmlDocument doc = one.getXmlDocument();

public static XmlElement getNewXmlElement(string elementName, string
stringValue)
{
XmlElement element = doc.getXmlDocument().CreateElement("",
elementName, null);
element.InnerText = stringValue;
return element;
}
}

class test{

XmlDocument doc = one.getXmlDocument();
XmlElement e = two.getNewXmlElement("ReferencedOrder", "test");
doc.AppendChild(e);
doc.Save(Console.Out);
}

Error:
An unhandled exception of type 'System.ArgumentException' occurred in
system.xml.dll

Additional information: (translated from danish) The node to be inserted
comes from a different documentcontext

Does anyone know why ?
 
A

Ajay Kalra

I dont know about the exception problem but you should not initialize
static variable doc in two places. You are doing it in
getNewXMLDocuemnt and it gets initialized as part of the static
intializer.

Write a static constructor to initialize doc. You should prefer the
static constructor for initialization as you can catch exceptions in
it.

As for the exception problem, I can only guess that you are taking an
element from one document and appending it onto another document which
is likely to be a problem (this is my guess). You may want to clone or
something.
 
A

Ajay Kalra

Ignore my comment about exception guess as it appears you are using the
same doc to append the element.
 
T

Tor Bådshaug

The problem is that when you call doc.AppendChild, the XmlElement you are
appending does not belong to doc (it actually belongs to a similar but
separate XmlDocument instance). An XmlElement can only be appended or
inserted into an XmlDocument to which it belongs (which is the XmlDocument
through which it was created, unless it has been imported).
A quick fix is to import the XmlElement you are appending to doc before
appending it like this :

Instead of :
doc.AppendChild(e);
use this
doc.AppendChild(doc.ImportNode(e, true ) );

Alternatively, you might delve deeper into the problem. Observe that inside
the class there are several XmlDocument instances involved. First of all, an
XmlDocument is instantiated and assigned to a class variable when class
"two" is loaded. When you are calling two.getNewXmlElement, the new element
is created from the context of this instance (referred to by two's class
variable). Note that here I have to assume that your code (in
two.getNewXmlElement):
XmlElement element = doc.getXmlDocument().CreateElement("",
elementName, null);

actually should have been
XmlElement element = doc.CreateElement("", elementName, null);

However, as we return from two.getNewXmlElement and you are about to append
the newly created element in class test, note that "test" is operating on a
second, separate XmlDocument instance (created using one.getXmlDocument).

You may also note that your implementation of one.getXmlDocument always will
return a fresh XmlDocument instance as the private variable inside
one.getXmlDocument hides the class variable.

Hope this helps.

Tor Bådshaug
tor.badshaug (at) bekk.no.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top