小编nic*_*ald的帖子

按特定顺序编写 XML 属性和命名空间声明

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

<urn:Command complete="true" xmlns:urn="namespaceURI">
Run Code Online (Sandbox Code Playgroud)

所以我有一个元素Command ,一个命名空间,namespaceURI 一个前缀urn ,最后一个属性字符串,其中包含名称和completetrue ,但没有命名空间。

我为此编写的代码返回:

<urn:Command xmlns:urn="namespaceURI" complete="true">
Run Code Online (Sandbox Code Playgroud)

所以问题是我希望属性字符串位于 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 …
Run Code Online (Sandbox Code Playgroud)

c# xml xmlwriter xml-namespaces xml-attribute

4
推荐指数
1
解决办法
2397
查看次数

标签 统计

c# ×1

xml ×1

xml-attribute ×1

xml-namespaces ×1

xmlwriter ×1