tags:

views:

1442

answers:

2

How do I convert the xml in an XDocument to a MemoryStream?

+6  A: 

Have a look at the XDocument.WriteTo method, e.g.

MemoryStream ms = new MemoryStream();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;

using (XmlWriter xw = XmlWriter.Create(ms, xws))
{
    XDocument doc = new XDocument(
        new XElement("Child",
            new XElement("GrandChild", "some content")
        )
    );
    doc.WriteTo(xw);
}
dtb
Or .Save - but the example holds ;-p
Marc Gravell
@Marc What's the difference between WriteTo() and Save()?
Daniel Fortunov
@Daniel Fortunov: .Save provides more overloads, but all of these end up calling .WriteTo
dtb
A: 

VARIANT_BOOL bstatus; bool bRetVal = false; BSTR bstr;

wi_CharToBSTR(source , bstr);

// Load from Buffer
//
HRESULT hr = m_pXMLDomDoc->loadXML(bstr,&bstatus);
if (bstatus == VARIANT_TRUE) 
{
 // now that the document is loaded, initialize the root pointer
 m_pXMLDomDoc->get_documentElement(&m_pXMLRoot);
 bRetVal = true;
}

SysFreeString(bstr);
Chand
-1: XDocument is LINQ to XML's API. You seem to be using MSXML, not even .NET or C#.
Richard