473,748 Members | 10,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CreateXmlDe claration("1.0" ,
"iso-8859-1", null); //"iso-8859-1"
doc.InsertBefor e(xmlDecl, doc.DocumentEle ment);
return doc;
}
}

class two{

private static XmlDocument doc = one.getXmlDocum ent();

public static XmlElement getNewXmlElemen t(string elementName, string
stringValue)
{
XmlElement element = doc.getXmlDocum ent().CreateEle ment("",
elementName, null);
element.InnerTe xt = stringValue;
return element;
}
}

class test{

XmlDocument doc = one.getXmlDocum ent();
XmlElement e = two.getNewXmlEl ement("Referenc edOrder", "test");
doc.AppendChild (e);
doc.Save(Consol e.Out);
}

Error:
An unhandled exception of type 'System.Argumen tException' 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 1867
I dont know about the exception problem but you should not initialize
static variable doc in two places. You are doing it in
getNewXMLDocuem nt 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.getNewXmlEl ement, 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.getNewXmlEl ement):
XmlElement element = doc.getXmlDocum ent().CreateEle ment("",
elementName, null);


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

However, as we return from two.getNewXmlEl ement 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.getXmlDocum ent).

You may also note that your implementation of one.getXmlDocum ent always will
return a fresh XmlDocument instance as the private variable inside
one.getXmlDocum ent 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
1624
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 include them simple in both? Greetz JC
4
4809
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 class, because that would give me access to different members than in the first instance and would not behave as one global object. How can I do that, any guess? Thanks.
7
1774
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 common property called DataPath cls1.DataPath = strPath
3
5470
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
1275
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 both projects, and each different project has its own special funtionality. They are both very similar and exist together, where the irrelevent menu items for example are greyed in one and not the other. Each needs a different icon and title...
3
1534
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 = "table1"; } Now I would like to do something like this: $classes=array("MyClass1","MyClass2","MyClass3"); foreach ($classes as $item)
1
1485
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 will this also work across different domains or workgroups? I've not looked into the authenticate issues thus far, so would appreciate it if someone could let me know! Thanks in advance
1
1513
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
1666
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 name="urn:schemas-microsoft-com:office:infopath:PublishedDiagnosticTests:-myXSD-2008-06-13T05-43-58" solutionVersion="1.0.0.75" productVersion="12.0.0.0" PIVersion="1.0.0.0" href="http://sharepointqtc:8558/formservertemplates/PublishedDiagnosticTests.xsn"?> <?mso-application...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.