简体   繁体   English

如何从 XDocument 获取 Xml 作为字符串?

[英]How to get Xml as string from XDocument?

I am new to LINQ to XML.我是 LINQ to XML 的新手。 After you have built XDocument , how do you get the OuterXml of it like you did with XmlDocument ?构建XDocument之后,如何像使用XmlDocument一样获得它的OuterXml

You only need to use the overridden ToString() method of the object:您只需要使用对象的重写 ToString() 方法:

XDocument xmlDoc ...
string xml = xmlDoc.ToString();

This works with all XObjects, like XElement, etc.这适用于所有 XObject,如 XElement 等。

I don't know when this changed, but today (July 2017) when trying the answers out, I got我不知道这什么时候改变了,但是今天(2017 年 7 月)在尝试答案时,我得到了

"System.Xml.XmlDocument" "System.Xml.XmlDocument"

Instead of ToString() , you can use the originally intended way accessing the XmlDocument content: writing the xml doc to a stream.代替ToString() ,您可以使用最初预期的方式访问XmlDocument内容:将 xml doc 写入流。

XmlDocument xml = ...;
string result;

using (StringWriter writer = new StringWriter())
{
  xml.Save(writer);
  result = writer.ToString();
}

Doing XDocument.ToString() may not get you the full XML.执行 XDocument.ToString() 可能无法获得完整的 XML。

In order to get the XML declaration at the start of the XML document as a string, use the XDocument.Save() method:为了以字符串形式获取 XML 文档开头的 XML 声明,请使用 XDocument.Save() 方法:

    var ms = new MemoryStream();
    using (var xw = XmlWriter.Create(new StreamWriter(ms, Encoding.GetEncoding("ISO-8859-1"))))
        new XDocument(new XElement("Root", new XElement("Leaf", "data"))).Save(xw);
    var myXml = Encoding.GetEncoding("ISO-8859-1").GetString(ms.ToArray());

Use ToString() to convert XDocument into a string:使用 ToString() 将 XDocument 转换为字符串:

string result = string.Empty;
XElement root = new XElement("xml",
    new XElement("MsgType", "<![CDATA[" + "text" + "]]>"),
    new XElement("Content", "<![CDATA[" + "Hi, this is Wilson Wu Testing for you! You can ask any question but no answer can be replied...." + "]]>"),
    new XElement("FuncFlag", 0)
);
result = root.ToString();

Several responses give a slightly incorrect answer.几个回答给出了一个稍微不正确的答案。

  • XDocument.ToString() omits the XML declaration (and, according to @Alex Gordon, may return invalid XML if it contains encoded unusual characters like &amp; ). XDocument.ToString()省略了 XML 声明(并且,根据@Alex Gordon,如果它包含编码的异常字符,如&amp; ,则可能返回无效的 XML)。
  • Saving XDocument to StringWriter will cause .NET to emit encoding="utf-16" , which you most likely don't want (if you save XML as a string, it's probably because you want to later save it as a file, and de facto standard for saving files is UTF-8 - .NET saves text files as UTF-8 unless specified otherwise).XDocument保存到StringWriter将导致 .NET 发出encoding="utf-16" ,这是您很可能不想要的(如果您将 XML 保存为字符串,那可能是因为您想稍后将其保存为文件,并且 de保存文件的事实标准是 UTF-8 - .NET 将文本文件保存为 UTF-8,除非另有说明)。
  • @Wolfgang Grinfeld's answer is heading in the right direction, but it's unnecessarily complex. @Wolfgang Grinfeld 的答案正朝着正确的方向发展,但它不必要地复杂。

Use the following:使用以下内容:

  var memory = new MemoryStream();
  xDocument.Save(memory);
  string xmlText = Encoding.UTF8.GetString(memory.ToArray());

This will return XML text with UTF-8 declaration.这将返回带有 UTF-8 声明的 XML 文本。

I found this example in the Microsoft .NET 6 documentation for XDocument.Save method.我在XDocument.Save方法的 Microsoft .NET 6 文档中找到了这个示例。 I think it answers the original question (what is the XDocument equivalent for XmlDocument.OuterXml), and also addresses the concerns that others have pointed out already.我认为它回答了最初的问题(XmlDocument.OuterXml 的 XDocument 等价物是什么),并且还解决了其他人已经指出的问题。 By using the XmlWritingSettings you can predictably control the string output.通过使用 XmlWritingSettings,您可以预见性地控制字符串输出。

https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.save https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.save

StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
 
using (XmlWriter xw = XmlWriter.Create(sb, xws)) {
    XDocument doc = new XDocument(
        new XElement("Child",
            new XElement("GrandChild", "some content")
        )
    );
    doc.Save(xw);
}
Console.WriteLine(sb.ToString());

While @wolfgang-grinfeld's answer is technically correct (as it also produces the XML declaration, as opposed to just using .ToString() method), the code generated UTF-8 byte order mark (BOM), which for some reason XDocument.Parse(string) method cannot process and throws Data at the root level is invalid. Line 1, position 1.虽然@wolfgang-grinfeld 的回答在技术上是正确的(因为它还生成 XML 声明,而不是仅使用.ToString()方法),但代码生成了 UTF-8 字节顺序标记(BOM),出于某种原因XDocument.Parse(string)方法无法处理并抛出Data at the root level is invalid. Line 1, position 1. Data at the root level is invalid. Line 1, position 1. error. Data at the root level is invalid. Line 1, position 1.错误。

So here is a another solution without the BOM:所以这是另一个没有 BOM 的解决方案:

var utf8Encoding =
    new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);

using (var memory = new MemoryStream())
using (var writer = XmlWriter.Create(memory, new XmlWriterSettings
       {
           OmitXmlDeclaration = false,
           Encoding = utf8Encoding
       }))
{
    CompanyDataXml.Save(writer);
    writer.Flush();
    return utf8Encoding.GetString(memory.ToArray());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM