【问题标题】:Xdocument does not print declarationXdocument 不打印声明
【发布时间】:2009-06-29 19:54:20
【问题描述】:

我尝试使用 domainpeople.com API,但我需要使用 XML。

目前我有一个错误提示“未找到 apiProtocol”,我猜我的 Xml 文档格式不正确。

当前发送的xml是:

<apiProtocol version="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNameSpaceSchemaLocation="checkrequest.xsd">
  <checkRequest user="ifuzion" password="fish4gold121" reference="123456789">
    <domain name="google.com" /> 
  </checkRequest>
</apiProtocol>

显然&lt;?xml?&gt; 部分没有打印出来。

我的代码基本上类似于:

XDocument xDocument = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Books"));

(为了简单起见,我删除了我的代码,但结构完全相似)。

XDocument 没有打印出&lt;?xml?&gt; 部分有什么原因吗?似乎使用 XmlDocument 它可以工作,但不能使用 XDocument ...任何提示?

【问题讨论】:

    标签: asp.net xml c#-3.0 linq-to-xml


    【解决方案1】:

    您如何打印出XDocument 的内容?

    .ToString() 方法不包含 xml 标头,但 .Save() 方法包含。

    编辑:给出了相同的答案here

    【讨论】:

    • 是的,我刚刚看到我需要一个字符串编写器....除此之外,我似乎可以在编码类型中放入任何东西来声明它仍将使用 UTF-16...目前正在搜索解决这个问题
    • 您找到解决 UTF-16 问题的方法了吗?我也有同样的经历。我猜这是因为 .NET 中的字符串是 UTF-16,如果您不编码为字节序列,它将始终是 UTF-16?
    • 看起来 Mörk 的回答提供了一个潜在的解决方案 (stackoverflow.com/questions/1060164/…),但您必须在 XDeclaration.Encoding 字符串和实际输出 Encoding 格式之间手动转换。
    【解决方案2】:

    你如何保存它?如果我执行以下操作,xml 声明就会出现:

    XDocument xDocument = new XDocument(
        new XDeclaration("1.0", "UTF-8", "yes"),
        new XElement("Books"));
    xDocument.Save(@"c:\temp\file.xml");
    

    输出如下:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <Books />
    

    但是,如果我改为传递 XmlWriter 实例,则该 XmlWriter 的设置似乎会覆盖 XDocument 中所述的设置:

    XDocument xDocument = new XDocument(
        new XDeclaration("1.0", "UTF-8", "yes"),
        new XElement("Books"));
    StringBuilder sb = new StringBuilder();
    using (XmlWriter writer = XmlWriter.Create(sb))
    {
        xDocument.Save(writer);
    }
    Console.WriteLine(sb.ToString());
    

    输出如下:

    <?xml version="1.0" encoding="utf-16" standalone="yes"?><Books />
    

    请注意编码如何更改为“utf-16”并且缩进已更改。如果您添加一个 XmlWriterSettings 实例来指示编码(以及您想要控制的任何其他设置),您将获得更好的结果。以下代码符合您的预期:

    XDocument xDocument = new XDocument(
        new XDeclaration("1.0", "UTF-8", "yes"),
        new XElement("Books"));
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = Encoding.UTF8;
    settings.ConformanceLevel = ConformanceLevel.Document;
    settings.Indent = true;
    
    using (XmlWriter writer = XmlWriter.Create(@"c:\temp\xdocument.xml", settings))
    {
        xDocument.Save(writer);
    }
    

    输出:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <Books />
    

    【讨论】:

    • 写入文件时很好。请注意,如果您写入 StringBuilder、StringWriter 等,则执行 settings.Encoding = Encoding.UTF8 是不够的。您仍然会得到&lt;?xml version="1.0" encoding="utf-16" standalone="yes"?&gt;(带有16),因为.NET 中字符串的内部表示是UTF-16。
    【解决方案3】:

    序列化为字符串的解决方案:

    // Default encode as Utf8
    Encoding outputEncoding = new UTF8Encoding(/*bom*/false);
    
    // Try to use Xml encoding
    if (xml.Declaration != null &&
        xml.Declaration.Encoding.ToNonNull().ToLower() != System.Text.Encoding.UTF8.WebName)
    {
        outputEncoding = System.Text.Encoding.GetEncoding(xml.Declaration.Encoding);
    }
    
    using (var stream = new MemoryStream())
    {
        using (new XmlTextWriter(stream, outputEncoding))
        {
            xml.Save(stream);
        }
    
        return outputEncoding.GetString(stream.ToArray());
    }
    

    【讨论】:

      【解决方案4】:

      您是否尝试保存 XDocument?

      xDocument.Save("books.xml");
      

      看这个问题how-to-print-xml-version1-0-using-xdocument

      这里是文档:XDocument.Save Method

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-16
        • 2021-04-08
        • 2013-09-13
        • 1970-01-01
        相关资源
        最近更新 更多