本文介绍了XDocument.Save()无标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编辑的csproj文件,LINQ到XML和需要保存XML没有<?XML方式>

I am editing csproj files with Linq-to-XML and need to save the XML without the <?XML?> header.

由于XDocument.Save()缺少必要的选择,什么是做到这一点的最好方法是什么?

As XDocument.Save() is missing the necessary option, what's the best way to do this?

推荐答案

您可以用 XmlWriterSettings 做到这一点,并且文档保存到一个的XmlWriter

You can do this with XmlWriterSettings, and saving the document to an XmlWriter:

XDocument doc = new XDocument(new XElement("foo",
    new XAttribute("hello","world")));

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
StringWriter sw = new StringWriter();
using (XmlWriter xw = XmlWriter.Create(sw, settings))
// or to write to a file...
//using (XmlWriter xw = XmlWriter.Create(filePath, settings))
{
    doc.Save(xw);
}
string s = sw.ToString();

这篇关于XDocument.Save()无标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 14:57