C# 按特定顺序编写XML属性和命名空间声明
我正在尝试使用根元素创建XML文件:C# 按特定顺序编写XML属性和命名空间声明,c#,xml,xml-namespaces,xmlwriter,xml-attribute,C#,Xml,Xml Namespaces,Xmlwriter,Xml Attribute,我正在尝试使用根元素创建XML文件: <urn:Command complete="true" xmlns:urn="namespaceURI"> 所以问题是我希望属性字符串在XML文件中的名称空间定义之前,而我在这个网站上找不到类似的问题 我尝试过编写一个带有前缀和名称空间的StartElement,然后编写一个不带名称空间的AttributeString,这将返回根元素,该根元素首先具有定义的名称空间,然后是属性字符串。 我也尝试过只定义一个开始元素,然后定义两个属性字符串,但
<urn:Command complete="true" xmlns:urn="namespaceURI">
所以问题是我希望属性字符串在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属性和名称空间声明顺序应该无关紧要
根据:
请注意,属性规范在开始标记或
空元素标记不重要
命名空间声明类似于属性(,节)
[定义:名称空间(或者更准确地说,名称空间绑定)是
使用一系列保留属性声明。此类属性的名称必须为xmlns或以xmlns:开头。这些
与任何其他XML属性一样,属性可以直接或间接提供
通过。]
以同样不重要的顺序
因此,任何符合规范的XML工具或库都不会关心XML属性和XML命名空间声明的顺序,您也不应该这样做。
这就是为什么XML库通常不提供约束属性顺序的方法,而尝试这样做几乎总是表明您做错了什么
…除了很少需要的规范化应用程序
XML建议都将考虑属性排序和命名空间声明排序是无关紧要的,但是在应用程序中或如果您的应用程序不可避免地需要属性排序时,请参阅。需要为数字签名()建立词汇平等/不平等就是这样一个例外
另请参见(但仅当您必须对XML属性和名称空间声明进行排序时):- .NET
- 爪哇
- XSLT
- 多平台
- :
和saxon:attributeorder?
saxon:canonical?
- :
`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;`