在C#中,将字符串格式化为XML的最佳方法是什么?

Joh*_*nyM 40 c# xml string formatting string-formatting

我正在使用C#创建一个轻量级编辑器,并希望知道将字符串转换为格式良好的XML字符串的最佳方法.我希望C#库中有一个公共方法,比如"public bool FormatAsXml(string text,out string formattedXmlText)",但它可能不那么容易,是吗?

非常具体地,"SomeMethod"方法必须是什么才能产生下面的输出?

string unformattedXml;
string formattedXml;

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"
formattedXml = SomeMethod(unformattedXml);

Console.WriteLine(formattedXml);
Run Code Online (Sandbox Code Playgroud)

输出:

<?xml version="1.0"?>
  <book id="123">
    <author>Lewis, C.S.</author>
    <title>The Four Loves</title>
  </book>
Run Code Online (Sandbox Code Playgroud)

Won*_*nko 71

string unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>";
string formattedXml = XElement.Parse(unformattedXml).ToString();
Console.WriteLine(formattedXml);
Run Code Online (Sandbox Code Playgroud)

输出:

<book>
  <author>Lewis, C.S.</author>
  <title>The Four Loves</title>
</book>
Run Code Online (Sandbox Code Playgroud)

Xml声明不是由ToString()输出的,而是由Save()输出的......

  XElement.Parse(unformattedXml).Save(@"C:\doc.xml");
  Console.WriteLine(File.ReadAllText(@"C:\doc.xml"));
Run Code Online (Sandbox Code Playgroud)

输出:

<?xml version="1.0" encoding="utf-8"?>
<book>
  <author>Lewis, C.S.</author>
  <title>The Four Loves</title>
</book>
Run Code Online (Sandbox Code Playgroud)


Ash*_*Ash 15

不幸的是,它不像FormatXMLForOutput方法那么容易,这是微软在这里谈论的;)

无论如何,从.NET 2.0开始,推荐的方法是使用XMlWriterSettingsClass设置格式,而不是直接在XmlTextWriter对象上设置属性. 有关详细信息,请参阅此MSDN页面.它说:

"在.NET Framework 2.0版中,建议的做法是使用XmlWriter.Create方法和XmlWriterSettings类创建XmlWriter实例.这使您可以充分利用此版本中引入的所有新功能.有关更多信息,请参阅创建XML Writer."

以下是推荐方法的示例:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("    ");
using (XmlWriter writer = XmlWriter.Create("books.xml", settings))
{
    // Write XML data.
    writer.WriteStartElement("book");
    writer.WriteElementString("price", "19.95");
    writer.WriteEndElement();
    writer.Flush();
}
Run Code Online (Sandbox Code Playgroud)


Jas*_*son 13

使用新的System.Xml.Linq命名空间(System.Xml.Linq程序集),您可以使用以下命令:

string theString = "<nodeName>blah</nodeName>";
XDocument doc = XDocument.Parse(theString);
Run Code Online (Sandbox Code Playgroud)

您还可以使用以下命令创建片段:

string theString = "<nodeName>blah</nodeName>";
XElement element = XElement.Parse(theString);
Run Code Online (Sandbox Code Playgroud)

如果字符串还不是XML,您可以执行以下操作:

string theString = "blah";
//creates <nodeName>blah</nodeName>
XElement element = new XElement(XName.Get("nodeName"), theString); 
Run Code Online (Sandbox Code Playgroud)

在最后一个示例中需要注意的是XElement将对所提供的字符串进行XML编码.

我强烈推荐新的XLINQ课程.它们的重量更轻,并且更容易使用大多数现有的XmlDocument相关类型.


Dan*_*ley 9

假设您只是想重新格式化XML文档以将新节点放在新行上并添加缩进,那么,如果您使用的是.NET 3.5或更高版本,则最佳解决方案是使用XDocument解析然后输出,类似于:

string unformattedXml;
string formattedXml;

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>";
formattedXml = System.Xml.Linq.XDocument.Parse(unformattedXml).ToString();

Console.WriteLine(formattedXml);
Run Code Online (Sandbox Code Playgroud)

整洁的胡?

然后,这应该重新格式化XML节点.

要使用以前版本的框架执行此操作需要更多的工作,因为没有内置函数来重新计算空白.

事实上,使用pre-Linq类来实现它将是:

string unformattedXml;
string formattedXml;

unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(unformattedXml);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Xml.XmlWriter xw = System.Xml.XmlTextWriter.Create(sb, new System.Xml.XmlWriterSettings() { Indent = true });
doc.WriteTo(xw);
xw.Flush();
formattedXml = sb.ToString();
Console.WriteLine(formattedXml);
Run Code Online (Sandbox Code Playgroud)


def*_*ted 5

听起来您想要将XML加载到XmlTextWriter对象中并设置Formatting和Indentation属性:

writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';
Run Code Online (Sandbox Code Playgroud)