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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/194944/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:33:33  来源:igfitidea点击:

In C#, what is the best method to format a string as XML?

c#xmlstringformattingstring-formatting

提问by JohnnyM

I am creating a lightweight editor in C# and would like to know the best method for converting a string into a nicely formatted XML string. I would hope that there's a public method in the C# library like "public bool FormatAsXml(string text, out string formattedXmlText)", but it couldn't be that easy, could it?

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

Very specifically, what would the method "SomeMethod" have to be that would produce the output below?

非常具体地说,“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);

Output:

输出:

<?xml version="1.0"?>
  <book id="123">
    <author>Lewis, C.S.</author>
    <title>The Four Loves</title>
  </book>

采纳答案by Wonko

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);

Output:

输出:

<book>
  <author>Lewis, C.S.</author>
  <title>The Four Loves</title>
</book>

The Xml Declaration isn't output by ToString(), but it is by Save() ...

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

  XElement.Parse(unformattedXml).Save(@"C:\doc.xml");
  Console.WriteLine(File.ReadAllText(@"C:\doc.xml"));

Output:

输出:

<?xml version="1.0" encoding="utf-8"?>
<book>
  <author>Lewis, C.S.</author>
  <title>The Four Loves</title>
</book>

回答by rp.

Is the string valid XML? Do you mean how can you convert an XML string into an XML document? If so, do this:

字符串是有效的 XML 吗?您的意思是如何将 XML 字符串转换为 XML 文档?如果是这样,请执行以下操作:

XmlDocument xml = new XmlDocument();

xml.LoadXml( YourString );

回答by defeated

It sounds like you want to load the XML into an XmlTextWriterobjects and set the Formatting and Indentation properties:

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

writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';

回答by Ash

Unfortunately no, it's not as easy as a FormatXMLForOutput method, this is Microsoft were talking about here ;)

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

Anyway, as of .NET 2.0, the recommended approach is to use the XMlWriterSettingsClass to set up formatting, as opposed to setting properties directly on the XmlTextWriter object. See this MSDN pagefor more details. It says:

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

"In the .NET Framework version 2.0 release, the recommended practice is to create XmlWriter instances using the XmlWriter.Create method and the XmlWriterSettings class. This allows you to take full advantage of all the new features introduced in this release. For more information, see Creating XML Writers. "

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

Here is an example of the recommended approach:

以下是推荐方法的示例:

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();
}

回答by Jason Hymanson

Using the new System.Xml.Linq namespace (System.Xml.Linq Assembly) you can use the following:

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

string theString = "<nodeName>blah</nodeName>";
XDocument doc = XDocument.Parse(theString);

You can also create a fragment with:

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

string theString = "<nodeName>blah</nodeName>";
XElement element = XElement.Parse(theString);

If the string is not yet XML, you can do something like this:

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

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

Something to note in this last example is that XElement will XML Encode the provided string.

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

I highly recommend the new XLINQ classes. They are lighter weight, and easier to user that most of the existing XmlDocument-related types.

我强烈推荐新的 XLINQ 类。与大多数现有的 XmlDocument 相关类型相比,它们重量更轻,更易于用户使用。

回答by sbeskur

If you just need to escape XML characters the following might be useful:

如果您只需要转义 XML 字符,以下内容可能有用:

string myText = "This & that > <> &lt;";
myText = System.Security.SecurityElement.Escape(myText);

回答by JohnnyM

Jason's approach is the simplest. Here's the method:

Jason的方法是最简单的。这是方法:

private static string FormatXmlString(string xmlString)
{
    System.Xml.Linq.XElement element = System.Xml.Linq.XElement.Parse(xmlString);
    return element.ToString();
}

回答by Daniel Bradley

Assuming your're simply wanting to re-format an XML document to put new nodes on new lines and add indenting, then, if you are using .NET 3.5 or above then the best solution is to parse then output with XDocument, somthing like:

假设您只是想重新格式化 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);

Neat hu?

整洁的胡?

This should then re-format the XML nodes.

然后应该重新格式化 XML 节点。

To do this with previous versions of the framework requires a lot more legwork as there is no built in functions to re-calculate the whitespace.

要使用先前版本的框架来做到这一点需要更多的工作,因为没有内置函数来重新计算空格。

In fact, to do it using pre-Linq classes would be:

事实上,使用 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);

回答by radarbob

System.Xml.Linq.XElement.ToString() Automatically Formats!

System.Xml.Linq.XElement.ToString() 自动格式化!

XElement formattedXML = new XElement.Parse(unformattedXmlString);
Console.WriteLine(formattedXML.ToString());

回答by midspace

In Framework 4.0 it issimple.

在 Framework 4.0 中,这简单。

var unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>";
var xdoc = System.Xml.Linq.XDocument.Parse(unformattedXml);
var formattedXml = (xdoc.Declaration != null ? xdoc.Declaration + "\r\n" : "") + xdoc.ToString();
Console.WriteLine(formattedXml);

This adds in the required indentation, and maintains the Xml Declaration.

这会添加所需的缩进,并维护 Xml 声明

<?xml version="1.0"?>
<book>
  <author>Lewis, C.S.</author>
  <title>The Four Loves</title>
</book>