当我使用 XmlTextWriter 类时,为什么 V 形的编码不同?
当我调用 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();
然后我看到它正在创建这个:
< ?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 将第一个元素编码到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为这是您唯一使用
WriteValue
的地方XML 声明不是元素。它是一条处理指令。由于版本 1.0 和 UTF-8 编码是默认值,因此最简单的方法就是完全忽略它。它不会对文档的含义产生影响,但可以节省您的时间和一些字节。
如果您确实想包含它,请使用 WriteStartDocument
Because it is the only place you used
WriteValue
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
不要使用
WriteValue()
,它会进行编码。请改用 WriteProcessingInstruction。MSDN 示例:
Do not use
WriteValue()
, it does the encoding. Use WriteProcessingInstruction instead.MSDN example: