I ran into a bit of a road block when I had a complex XML code structure already already typed as a string. I didn’t have the time to convert the entire string (which contained well formatted XML) into an XMLDocument structure. However, I needed my Web Service to return true XML in the form of an XMLDocument. So…. here is how I did it.

The code was nothing come through the blogger very well since it was .Net and XML code combined, so here is another copy of the code snippet.

[WebMethod] public XmlDocument ExampleWS()
XmlDocument xDoc = new XmlDocument();
string xmlAsString = @"abc";

// --- magical two lines of code, run in the string as a Memory Stream
// --- thus making XMLDoc think it is a file containing XML!
byte[] byteArray = Encoding.ASCII.GetBytes(xmlAsString);
MemoryStream stream = new MemoryStream(byteArray);

return xDoc;