C# 将 xmlwriter 写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15276297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write out xmlwriter to file
提问by GurdeepS
I have an xmlwriter object used in a method. I'd like to dump this out to a file to read it. Is there a straightforward way to do this?
我在方法中使用了一个 xmlwriter 对象。我想将其转储到文件中以读取它。有没有直接的方法来做到这一点?
Thanks
谢谢
回答by user2140261
Use this code
使用此代码
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
// Add a price element.
XmlElement newElem = doc.CreateElement("price");
newElem.InnerText = "10.95";
doc.DocumentElement.AppendChild(newElem);
// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter(@"C:\data.xml", null);
writer.Formatting = Formatting.Indented;
doc.Save(writer);
As found on MSDN: http://msdn.microsoft.com/en-us/library/z2w98a50.aspx
在 MSDN 上找到:http: //msdn.microsoft.com/en-us/library/z2w98a50.aspx
回答by Darin Dimitrov
One possibility is to set the XmlWriter to output to a text file:
一种可能性是将 XmlWriter 设置为输出到文本文件:
using (var writer = XmlWriter.Create("dump.xml"))
{
...
}