c# - 以特定顺序编写 XML 属性和命名空间声明

标签 c# xml xml-namespaces xmlwriter xml-attribute

我正在尝试制作一个带有根元素的 XML 文件:

<urn:Command complete="true" xmlns:urn="namespaceURI">

所以我有一个元素 Command 命名空间 namespaceURI 前缀 urn 最后是名称为 complete 的属性字符串,值为 true 且没有命名空间。

我为此编写的代码返回:

<urn:Command xmlns:urn="namespaceURI" complete="true">

所以问题是我希望属性字符串位于 XML 文件中的命名空间定义之前,但我在该网站上找不到类似的问题。

我试过编写一个带有前缀和命名空间的 StartElement,然后编写一个没有命名空间的 AttributeString,这将首先返回具有已定义命名空间的根元素,然后是属性字符串。 我也试过只定义一个开始元素,然后定义两个属性字符串,但我找不到将前缀写入开始元素的方法。

这是我的原始代码,它首先返回带有命名空间定义的根元素,然后是属性定义:

`Dim Writer as System.Xml.XmlWriter;
dim writerSettings  as System.Xml.XmlWriterSettings;
dim basePath as string;
dim source as string;
dim destination as string;

writerSettings = new System.Xml.XmlWriterSettings();
'writerSettings.ConformanceLevel= false;
'writerSettings.Encoding = new System.Text.UTF8Encoding(false);
writerSettings.OmitXmlDeclaration = false;

basePath = System.IO.Path.Combine("\\wnlcuieb502\WEI\Outbound","RolexSet");
source  = System.IO.Path.Combine(basePath,"\\wnlcuieb502\WEI\Outbound","TEST.XML");

Writer = System.Xml.XmlWriter.Create(source,writerSettings);
Writer.WriteStartDocument();

Writer.WriteStartElement("urn","SetPackagingOrder","urn:laetus.com:ws:tts:mes");
Writer.WriteAttributeString("complete",null,"true");
Writer.WriteEndElement();
Writer.WriteEndDocument();
Writer.dispose();


try
    destination = System.IO.Path.Combine(basePath,"TEST.XML");
    while not System.IO.File.Exists(destination)        
        System.IO.File.Move(source,destination);
    endwhile;
catch
    LogError(Me.HierarchicalName + ": Could not move XML file: "+ "TEST.XML" +" from " + source + " to " + destination + ", Error: " + error.Message);
endtry;`

最佳答案

XML 属性和命名空间声明顺序无关紧要

Attribute order is insignificant根据 XML Recommendation :

Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

命名空间声明类似于属性(W3C Namespaces in XML Recommendation,部分3 Declaring Namespaces),

[Definition: A namespace (or more precisely, a namespace binding) is declared using a family of reserved attributes. Such an attribute's name must either be xmlns or begin xmlns:. These attributes, like any other XML attributes, may be provided directly or by default. ]

具有同样无关紧要的顺序。

因此,没有任何一致的 XML 工具或库会关心 XML 属性和 XML namespace 声明的顺序,您也不应该关心。

这就是为什么 XML 库通常不提供限制属性排序的方法,而尝试这样做几乎总是表明您做错了什么。


...除了很少需要的规范化应用程序

XML 建议都将属性排序和 namespace 声明排序视为无关紧要,但请参阅 section on attribute processingXML Normalization RecommendationCanonical XML Recommendation如果您的应用程序不可避免地需要属性排序。需要为数字签名 (XML Signature Syntax and Processing Version 1.1) 建立词汇相等/不等式就是这样的一个异常(exception)。

另请参阅(但如果您绝对必须订购 XML 属性和 namespace 声明):

关于c# - 以特定顺序编写 XML 属性和命名空间声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57756005/

相关文章:

sql - 在 SQL Server 中给定以下 XML,如何获取值?

c# - 如何删除 XDocument 中非根节点的 xmlns 属性?

c# - P/Invoke 是否执行 DLL 然后将其关闭?

c# - 如何通过单选按钮在sql server中插入值

javascript - 在具有某些参数的 xsl 中调用 javascript 函数

xml - 大量验证失败,类型为 xs :integer

c# - 如何在 xaml 和 resx 中使用 GNU gettext

javascript - AJAX post 请求报告失败,但实际上成功

xml - 如果许多条目具有相同的节点名称,如何正确定位 xml 节点?

java - getNodeName、getLocalName 未返回预期值