XmlWriter to Write to a String Instead of to a File

前端 未结 6 2019
星月不相逢
星月不相逢 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:33

    Well I think the simplest and fastest solution here would be just to:

    StringBuilder sb = new StringBuilder();
    
    using (var writer = XmlWriter.Create(sb, settings))
    {
        ... // Whatever code you have/need :)
    
        sb = sb.Replace("encoding=\"utf-16\"", "encoding=\"utf-8\""); //Or whatever uft you want/use.
        //Before you finally save it:
        File.WriteAllText("path\\dataName.xml", sb.ToString());
    }
    

提交回复
热议问题