使用 xmlwriter 在 C# 中写入空 xml 元素
我使用 xmlwriter 来编辑 xml 文件,但我需要它来维护特定的格式,以便将来与其他 xml 文件进行比较。
我使用以下代码来编写空元素:
w.WriteStartElement("description");
w.WriteEndElement();
xml 文件中的结果是:
通常没问题,但我需要它看起来像这样:
< code>
“description”后不带空格字符。
有什么办法可以做到吗?
I'm using xmlwriter in order to edit xml files, but I need it to maintain a specific format for future comparisons with other xml files.
I'm using the following code to write empty elements:
w.WriteStartElement("description");
w.WriteEndElement();
the result in the xml file is:
<description />
which would normally will be ok, but I need It to look like that:
<description/>
without the space character after "description".
Is there any way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XML 不是文本。规则不同。通常,不能使用文本比较来比较 XML 文件。您给出的两个示例是相同的 XML,但正如您所指出的,它们是不同的文本。
您可以通过预处理要比较的文件来开始解决此问题,例如将所有属性按相同顺序排列,也许每行一个属性;通过将元素放入规范的顺序,对相同的命名空间使用相同的前缀等。然后您必须告诉您的文本比较工具忽略空格等。
XML is not text. The rules are different. In general, you cannot use a text comparison to compare XML files. The two examples you gave are identical XML, yet, as you noted, they are different text.
You can begin to approach this by pre-processing the files to be compared to do things like put all attributes in the same order, perhaps one attribute per line; by placing elements into a canonical order, using the same prefix for the same namespace, etc. You then have to tell your text comparison tool to ignore whitespace, etc.
查看
XmlWriter.WriteRaw
< /a>,例如:Look at
XmlWriter.WriteRaw
, e.g.:这两种形式都是有效的 XML,无论您使用什么系统都应该能够解析这两种形式,以免它不是 XML 解析器。
我现在不在电脑前,但我觉得这会起作用:
Both of these forms are valid XML, and whatever system your using should be able to parse both forms, lest it not be an XML parser.
I'm not at my computer right now, but I feel like this would work: