强制 XDocument 使用 UTF-8 编码写入字符串

发布于 2024-09-26 06:59:51 字数 1413 浏览 2 评论 0原文

我希望能够使用声明和 UTF-8 编码将 XML 写入字符串。这似乎很难完成。

我已经阅读了一些内容并尝试了一些流行的答案,但它们都有问题。我当前的代码正确输出为 UTF-8,但不保持 XDocument 的原始格式(即缩进/空格)!

有人可以提供一些建议吗?

XDocument xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), xelementXML);

MemoryStream ms = new MemoryStream();
using (XmlWriter xw = new XmlTextWriter(ms, Encoding.UTF8))
{
    xml.Save(xw);
    xw.Flush();

    StreamReader sr = new StreamReader(ms);
    ms.Seek(0, SeekOrigin.Begin);

    String xmlString = sr.ReadToEnd();
}

XML 要求格式与 .ToString() 的格式相同,即

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
    <node>blah</node>
</root>

我当前看到的是

<?xml version="1.0" encoding="utf-8" standalone="yes"?><root><node>blah</node></root>

Update 我已经设法通过添加 XmlTextWriter 设置来使其工作......但它看起来非常笨重!

MemoryStream ms = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Indent = true;
using (XmlWriter xw = XmlTextWriter.Create(ms, settings))
{
    xml.Save(xw);
    xw.Flush();

    StreamReader sr = new StreamReader(ms);
    ms.Seek(0, SeekOrigin.Begin);
    String blah = sr.ReadToEnd();
}

I want to be able to write XML to a String with the declaration and with UTF-8 encoding. This seems mighty tricky to accomplish.

I have read around a bit and tried some of the popular answers for this but the they all have issues. My current code correctly outputs as UTF-8 but does not maintain the original formatting of the XDocument (i.e. indents / whitespace)!

Can anyone offer some advice please?

XDocument xml = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), xelementXML);

MemoryStream ms = new MemoryStream();
using (XmlWriter xw = new XmlTextWriter(ms, Encoding.UTF8))
{
    xml.Save(xw);
    xw.Flush();

    StreamReader sr = new StreamReader(ms);
    ms.Seek(0, SeekOrigin.Begin);

    String xmlString = sr.ReadToEnd();
}

The XML requires the formatting to be identical to the way .ToString() would format it i.e.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
    <node>blah</node>
</root>

What I'm currently seeing is

<?xml version="1.0" encoding="utf-8" standalone="yes"?><root><node>blah</node></root>

Update
I have managed to get this to work by adding XmlTextWriter settings... It seems VERY clunky though!

MemoryStream ms = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.ConformanceLevel = ConformanceLevel.Document;
settings.Indent = true;
using (XmlWriter xw = XmlTextWriter.Create(ms, settings))
{
    xml.Save(xw);
    xw.Flush();

    StreamReader sr = new StreamReader(ms);
    ms.Seek(0, SeekOrigin.Begin);
    String blah = sr.ReadToEnd();
}

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

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

发布评论

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

评论(3

对你的占有欲 2024-10-03 06:59:51

试试这个:

using System;
using System.IO;
using System.Text;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        XDocument doc = XDocument.Load("test.xml",
                                       LoadOptions.PreserveWhitespace);
        doc.Declaration = new XDeclaration("1.0", "utf-8", null);
        StringWriter writer = new Utf8StringWriter();
        doc.Save(writer, SaveOptions.None);
        Console.WriteLine(writer);
    }

    private class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding { get { return Encoding.UTF8; } }
    }
}

当然,您还没有向我们展示如何构建文档,这使得测试变得困难...我刚刚尝试过手动构建的 XDocument ,其中包含相关的空白也是如此。

Try this:

using System;
using System.IO;
using System.Text;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        XDocument doc = XDocument.Load("test.xml",
                                       LoadOptions.PreserveWhitespace);
        doc.Declaration = new XDeclaration("1.0", "utf-8", null);
        StringWriter writer = new Utf8StringWriter();
        doc.Save(writer, SaveOptions.None);
        Console.WriteLine(writer);
    }

    private class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding { get { return Encoding.UTF8; } }
    }
}

Of course, you haven't shown us how you're building the document, which makes it hard to test... I've just tried with a hand-constructed XDocument and that contains the relevant whitespace too.

_畞蕅 2024-10-03 06:59:51

尝试 XmlWriterSettings:

XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = false;
xws.Indent = true;

并像这样传递它

using (XmlWriter xw = XmlWriter.Create(sb, xws))

Try XmlWriterSettings:

XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = false;
xws.Indent = true;

And pass it on like

using (XmlWriter xw = XmlWriter.Create(sb, xws))
故事↓在人 2024-10-03 06:59:51

另请参阅https://stackoverflow.com/a/3288376/1430535

return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();

See also https://stackoverflow.com/a/3288376/1430535

return xdoc.Declaration.ToString() + Environment.NewLine + xdoc.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文