当我使用 XmlTextWriter 类时,为什么 V 形的编码不同?

发布于 2024-11-16 18:34:23 字数 1870 浏览 5 评论 0原文

当我调用 XMLTextWriter 时,似乎发生了一些有趣的行为...

XmlTextWriter writer = new XmlTextWriter(targetFileName, Encoding.UTF8);

writer.WriteValue("< ?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.WriteString(Environment.NewLine);
writer.WriteStartElement("video","UploadXsd");
writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("title");
        writer.WriteString(Environment.NewLine);
        writer.WriteString(title);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("description");
    writer.WriteString(Environment.NewLine);
        writer.WriteString(description);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("contributor");
    writer.WriteString(Environment.NewLine);
        writer.WriteString(contributor);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("subject");
    writer.WriteString(Environment.NewLine);
        writer.WriteString(subject);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
writer.WriteEndElement();
writer.WriteString(Environment.NewLine);

writer.Flush();
writer.Close();

然后我看到它正在创建这个:

&lt; ?xml version="1.0" encoding="UTF-8"?&gt;
<video xmlns="UploadXsd">
<title>
MyTitle
</title>
<description>
MyDescription
</description>
<contributor>
MyContributor
</contributor>
<subject>
MySubject
</subject>
</video>

Why has the writer 将第一个元素编码到 Html 中而不是其余元素?,更重要的是我该如何停止它在做这个吗?我只想创建第一个元素。

Seem to have some funny behaviour going on when I call the XMLTextWriter...

XmlTextWriter writer = new XmlTextWriter(targetFileName, Encoding.UTF8);

writer.WriteValue("< ?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.WriteString(Environment.NewLine);
writer.WriteStartElement("video","UploadXsd");
writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("title");
        writer.WriteString(Environment.NewLine);
        writer.WriteString(title);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("description");
    writer.WriteString(Environment.NewLine);
        writer.WriteString(description);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("contributor");
    writer.WriteString(Environment.NewLine);
        writer.WriteString(contributor);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
    writer.WriteStartElement("subject");
    writer.WriteString(Environment.NewLine);
        writer.WriteString(subject);
        writer.WriteString(Environment.NewLine);
    writer.WriteEndElement();
    writer.WriteString(Environment.NewLine);
writer.WriteEndElement();
writer.WriteString(Environment.NewLine);

writer.Flush();
writer.Close();

Then I see that it is creating this:

< ?xml version="1.0" encoding="UTF-8"?>
<video xmlns="UploadXsd">
<title>
MyTitle
</title>
<description>
MyDescription
</description>
<contributor>
MyContributor
</contributor>
<subject>
MySubject
</subject>
</video>

Why has the writer encoded the first element into Html but not the rest?, and more to the point how do I stop it doing this? I just want to create the first element.

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

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

发布评论

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

评论(2

山田美奈子 2024-11-23 18:34:23

为什么作者将第一个元素编码到 Html 中而不是其余元素?

因为这是您唯一使用 WriteValue 的地方

我该如何阻止它这样做?我只想创建第一个元素。

XML 声明不是元素。它是一条处理指令。由于版本 1.0 和 UTF-8 编码是默认值,因此最简单的方法就是完全忽略它。它不会对文档的含义产生影响,但可以节省您的时间和一些字节。

如果您确实想包含它,请使用 WriteStartDocument

Why has the writer encoded the first element into Html but not the rest?

Because it is the only place you used WriteValue

how do I stop it doing this? I just want to create the first element.

The XML declaration is not an element. It is a processing instruction. Since version 1.0 and a UTF-8 encoding is the default, the simplest thing to do is to just omit it entirely. It won't make a difference to the meaning of the document, but saves you time and a few bytes.

If you really want to include it, then use WriteStartDocument

花开柳相依 2024-11-23 18:34:23

不要使用 WriteValue() ,它会进行编码。请改用 WriteProcessingInstruction。

MSDN 示例

XmlWriter writer = XmlWriter.Create("output.xml");
writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-16'");
writer.WriteStartElement("root");
writer.Close();

Do not use WriteValue() , it does the encoding. Use WriteProcessingInstruction instead.

MSDN example:

XmlWriter writer = XmlWriter.Create("output.xml");
writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-16'");
writer.WriteStartElement("root");
writer.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文