XDocument 到字符串:如何在声明中省略编码?

发布于 2024-12-13 11:02:05 字数 751 浏览 1 评论 0原文

我正在为一个脑残的企业 XML API 编写一个包装器。我有一个 XDocument,需要将其转换为字符串。由于他们的 XML 解析器非常挑剔,甚至无法处理 XML 节点之间的空格,因此文档声明必须准确无误:

<?xml version="1.0"?>

但是,XDocument.Save() 方法总是在该声明:

<?xml version="1.0" encoding="utf-16"?>

过去一个小时在 Google 和 Stack 上寻找生成 XML 字符串的最佳方法,我能做的最好的事情是:

string result = xmlStringBuilder.ToString().Replace(@"encoding=""utf-16"", string.Empty));

我已经尝试过

xdoc.Declaration = new XDeclaration("1.0", null, null);

,并且确实成功地按照我想要的方式在 XDocument 中设置了声明;然而,当我调用 Save() 方法时,编码属性会神奇地返回到那里,无论我采取什么路线(使用 TextWriter、添加 XmlWriterSettings 等)。

有没有人有更好的方法来做到这一点,或者我的代码永远注定在可怕的字符串替换上方的注释中会有一段咆哮?

I'm writing a wrapper for a braindead enterprise XML API. I have an XDocument that I need to turn into a string. Due to the fact that their XML parser is so finicky that it cannot even handle whitespace between XML nodes, the document declaration MUST be EXACTLY:

<?xml version="1.0"?>

However, the XDocument.Save() method always adds an encoding attribute in that declaration:

<?xml version="1.0" encoding="utf-16"?>

With the past hour spent on Google and Stack looking for the best way to generate the XML string, the best I can do is:

string result = xmlStringBuilder.ToString().Replace(@"encoding=""utf-16"", string.Empty));

I've tried

xdoc.Declaration = new XDeclaration("1.0", null, null);

and that does succeed at setting the declaration in the XDocument the way I want it; however, when I call the Save() method, the encoding attribute gets magically thrown back in there, no matter what route I go (using TextWriter, adding XmlWriterSettings, etc.).

Does anyone have a better way to do this, or is my code forever doomed to have a paragraph of ranting in comments above the hideous string replace?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-12-20 11:02:05

那么接收端应该修复为使用 XML 解析器,而不是破坏 XML 语法的东西,而是使用 .NET 如果您想在发布时使用 XML 声明创建一个字符串,以下方法对我有用:

public class MyStringWriter : StringWriter
{
    public override Encoding Encoding
    {
        get
        {
            return null;
        }
    }
}

然后

    XDocument doc = new XDocument(
        new XDeclaration("1.0", null, null),
        new XElement("root", "test")
        );

    string xml;

    using (StringWriter msw = new MyStringWriter())
    {
        doc.Save(msw);
        xml = msw.ToString();
    }

    Console.WriteLine(xml);

输出

<?xml version="1.0"?>
<root>test</root>

Well the receiving end should be fixed to use an XML parser and not something that breaks with XML syntax but with .NET if you want to create a string with the XML declaration as you posted it the following approach works for me:

public class MyStringWriter : StringWriter
{
    public override Encoding Encoding
    {
        get
        {
            return null;
        }
    }
}

and then

    XDocument doc = new XDocument(
        new XDeclaration("1.0", null, null),
        new XElement("root", "test")
        );

    string xml;

    using (StringWriter msw = new MyStringWriter())
    {
        doc.Save(msw);
        xml = msw.ToString();
    }

    Console.WriteLine(xml);

outputs

<?xml version="1.0"?>
<root>test</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文