XmlWriter to Write to a String Instead of to a File

前端 未结 6 2015
星月不相逢
星月不相逢 2020-11-27 03:59

I have a WCF service that needs to return a string of XML. But it seems like the writer only wants to build up a file, not a string. I tried:

string nextXM         


        
6条回答
  •  粉色の甜心
    2020-11-27 04:35

    I know this is old and answered, but here is another way to do it. Particularly if you don't want the UTF8 BOM at the start of your string and you want the text indented:

    using (var ms = new MemoryStream())
    using (var x = new XmlTextWriter(ms, new UTF8Encoding(false)) 
                       { Formatting = Formatting.Indented })
    {
         // ...
         return Encoding.UTF8.GetString(ms.ToArray());
    }
    

提交回复
热议问题