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

same XmlDocument across different classes

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 ?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 1850
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.

-----------
Ajay Kalra
aj*******@yahoo.com

Nov 16 '05 #2
Ignore my comment about exception guess as it appears you are using the
same doc to append the element.

-----------
Ajay Kalra
aj*******@yahoo.com

Nov 16 '05 #3
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.
Nov 16 '05 #4

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

Similar topics

1
by: Jeroen Ceuppens | last post by:
Hi, I want to to include the same function in 2 different classes. class FormView.cs and FormLoad.cs, 2 of Windows.Forms these 2 classes have a couple of the same methods, what is the best way to...
4
by: MikeAth via DotNetMonster.com | last post by:
Hi all, I have two different classes and one instance of the Form. I would like to access the members of the Form instance in both classes without having to create a new instance in the other...
7
by: Ron | last post by:
Hello, I have 4 classes that use 4 DTS packages on 4 different tables. So I have Dim cls1 As New clsDTS1, cls2 As New clsDTS2 Dim cls3 As New clsDTS3, cls4 As New clsDTS4 Each class has a...
3
by: vivekian | last post by:
Hi, Is there a way to have partial classes across different namespaces ? namespace a.b.c { public partial class X { private void MyMethod () { } }
3
by: jeremy.todter | last post by:
I have a question...obviously I am developing an application, or two applications using c++ and openGL. They involve a basic structure where approximately 70 percent of the framework applies to...
3
by: juro | last post by:
Hi, I have a small problem: I'd like to call static variables from different classes - their names are stored in an array. Example of a class: class MyClass1{ public static $mysql_table =...
1
by: timmmahh | last post by:
I want to use the new encryption capabilities in .NET 2.0 remoting (e.g using the secure attribute). I've got my demo to work on a single and multiple server deployment in the same domain, but...
1
by: dmorand | last post by:
I'm trying to get my site to look somewhat decent across different resolutions. Should I change all of my fonts to use em's rather than px? Is that a good solution to fix my resolution issues?
0
by: Anni | last post by:
Hi, Can anyone please tell me how do I give same namespace for different infopath forms.? I have an XML formed by an infopath form, the XML is : <?mso-infoPathSolution...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.