Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Sunday, February 06, 2011

Annoyed by: System.Xml: The node to be inserted is from a different document context?

 Symptom:

You try to copy one or more XML Nodes (usually elements though) from one XMLDocument and another, and you get the following error:


System.Xml: The node to be inserted is from a different document context.

My Fix, Hint:

(Note from 1/21/2012: Try using the XDocument class and LINQtoXML, it's much easier to deal with!)

There is an official way to fix this using clone, import and InsertBefore or After. But that's too difficult to figure out, in my case I still get the above mentioned exceptions. I often do not know which node to insert (you are supposed to find the parent node then insert) or afraid to insert something at a wrong node level.

Rather than wasting a whole lot of time, I do this entirely in text using StringBuilder and such. You will find the following methods very handy.

XmlNode.OuterXML

  • This contains the Text representation of XML including the node itself and all of the children.

XmlNode.InnerXML

  • This contains the Text representation of XML node of all its children.

You know that you can build up a string using StringBuilder.Append(string) you can form the basis for the new XML document which some of the node data copied from another XML document WITHOUT having any issues with copies being prohibited between two documents.

As an added bonus, you can see exactly the new XML document being formed since it is in the string form.

Once you form the destination XML, there is a handy function LoadXML(string), and you are back to have the new XMLDocument. There absolutely is no need to create a StringReader or stream of any kind.



XmlDocument xd = new XmlDocument();
var sx = sDoc.ToString();
xd.LoadXml(sx);

Hope I gave you enough hint here, but if you need a more complete coding example, please post a comment and next time I come back here I will write one up for you.
 

Wednesday, August 18, 2010

Getting an XmlNode (or XmlElement) Out of XPathNodeIterator (C#)

Symptom:

You have a code you iterate through a group of Xml Nodes (for most people that's XmlElement). You have however noticed that once you get to a specific spot, there is no easy way to perform the complete node operations against the iterator since Casting of the iterator to XmlElement results in a compilation error.

So, you cannot cast the Current of the iterator like

XmlNode node = (XmlNode) i.Current;

(I really think this is a poor design myself since it goes against all our intuition.)

Solution:

Well, there actually is a way to "cast" this to an element, but the syntax is a bit more involved.

XmlNode node = ((IHasXmlNode) i.Current).GetNode();

Or in most cases you can cast this to XmlElement by

XmlElement node = (XmlElement) ((IHasXmlNode) i.Current).GetNode();

Saturday, July 17, 2010

Unable to switch the encoding at System.Data.SqlClient.SqlConnection

Symptom:

You have done the following:
  • You have set up an SQL table with XML data type in it.
  • You have a Strongly Typed DataSet
  • You try to write XML from a C# String
  • You get the following error
You have a DataSet unable to switch the encoding at System.Data.SqlClient.SqlConnection

What Fixed It For Me

The part of the original XML which went into the string had


in the XML heading

by simply switching this to


That fixed the problem.

Note:
  • In my case the real issue was that the actual strung was encoded in Unicode (UTF-16) but the XML header said utf-8. That mismatched.
  • In your case the actual string may be encoded in some other format such as utf-8. Be sure to check. If the real encoding is the issue then you could re-encode your string from what you have into UTF-16. Look for System.Text.Encoding class on MSDN for further information.