My blog has moved!

Visit http://blog.adamroderick.com/ and update your bookmarks.

Monday, July 21, 2008

Convert an XML string to an XMLNode using XmlDocumentFragment

In an orchestration, I was gathering messages representing Order Line Items. Once they were gathered, I would create the Order message, then inject the Order Line Items XML into the Order XML using a helper method. I did not want to create the Order message first, because I needed certain properties that I could only calculate after I had received all the Order Line Items (properties like NumberOfLines and OrderTotal).

To do this, I needed to store the Order Line Items as I received them, until I was ready to use them. I could not use XmlElement because it is not serializable. I tried using strings by storing the OuterXml property of the XmlElement, but I did not know how to convert the XML string back into XML. I could create a node, but it would not match the XmlDocument representing the Order. I found a good tip on a forum post at http://forums.asp.net/p/310492/310527.aspx suggesting the use of XmlDocumentFragment, which inherits from XmlNode (as do XmlElement and XmlDocument):


XmlDocumentFragment fragment = xmlDoc.CreateDocumentFragment();
fragment.InnerXml = xmlString;
parentNode.AppendChild(fragment);

Labels: ,

1 Comments:

Anonymous Anonymous said...

nice tip, thanks!

September 9, 2009 at 10:34 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home