C# 使用 XMLWriter 在其他属性之前写入 xmlns

标签 c# xml

这是创建 XML 的起始元素的代码:

writer.WriteStartElement("LIEFERUNG-AUSWI", "http://www.bundesbank.de/xmw/auswi/2013-01-01");      
writer.WriteAttributeString("xmlns", "aw", null, "http://www.bundesbank.de/xmw/auswi/2013-01-01");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "bbk", null, "http://www.bundesbank.de/xmw/2003-01-01");
writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.bundesbank.de/xmw/auswi/2013-01-01                                       BbkXmwAuswi_2013.xsd");
writer.WriteAttributeString(null, "version", null, "1.0");
writer.WriteAttributeString(null, "erstellzeit", null, Dat_DatZeit);
writer.WriteAttributeString(null, "stufe", null, "Produktion");

输出是这样的:

<LIEFERUNG-AUSWI xmlns:aw="http://www.bundesbank.de/xmw/auswi/2013-01-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bbk="http://www.bundesbank.de/xmw/2003-01-01" xsi:schemaLocation="http://www.bundesbank.de/xmw/auswi/2013-01-01 BbkXmwAuswi_2013.xsd" version="1.0" erstellzeit="2013-12-05T14:39:37" stufe="Produktion" xmlns="http://www.bundesbank.de/xmw/auswi/2013-01-01">

如何更改顺序以使 xmlns 属性成为第一个属性?应该是这样的:

<LIEFERUNG-AUSWI xmlns="http://www.bundesbank.de/xmw/auswi/2013-01-01" xmlns:aw="http://www.bundesbank.de/xmw/auswi/2013-01-01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bbk="http://www.bundesbank.de/xmw/2003-01-01" xsi:schemaLocation="http://www.bundesbank.de/xmw/auswi/2013-01-01 BbkXmwAuswi_2013.xsd" version="1.0" erstellzeit="2013-12-05T14:39:37" stufe="Produktion">

这个问题是相同的,但没有答案: Write xmlns element with attributes

最佳答案

您可以使用以下代码达到所需的结果:

writer.WriteStartElement("LIEFERUNG-AUSWI", "http://www.bundesbank.de/xmw/auswi/2013-01-01");
writer.WriteAttributeString("xmlns", "", null, "http://www.bundesbank.de/xmw/auswi/2013-01-01");
writer.WriteAttributeString("xmlns", "aw", null, "http://www.bundesbank.de/xmw/auswi/2013-01-01");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xmlns", "bbk", null, "http://www.bundesbank.de/xmw/2003-01-01");
writer.WriteAttributeString("xsi", "schemaLocation", null, "https://www.bundesbank.de/Redaktion/DE/Downloads/Service/Meldewesen/Aussenwirtschaft/Vordrucke/xsd/bbkxmwauswi_2013.xsd?__blob=publicationFile");
writer.WriteAttributeString(null, "version", null, "1.0");
writer.WriteAttributeString(null, "erstellzeit", null, DateTime.Now.ToString("s"));
writer.WriteAttributeString(null, "stufe", null, "Produktion");

但是请注意,aw 命名空间前缀指的是与默认命名空间相同的命名空间。它们都引用“http://www.bundesbank.de/xmw/auswi/2013-01-01”。解决方法是使用 aw´ 前缀写入根元素 (LIEFERUNG-AUSWI),_或_ 删除 aw` 前缀。

关于C# 使用 XMLWriter 在其他属性之前写入 xmlns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20401679/

相关文章:

c# - 正则表达式仅替换给定 xml 节点中的子字符串

c# - RabbitMQ 向所有消费者广播事件

c# - 优化 HTTP 请求和 CSV 文件上的多个拆分

c# - WinForm 中的 Application.DoEvents

ruby-on-rails - 在 Rails 3.1 中构建站点地图

xml - 对子树进行排序并将其存储在 xsl :variable 中

c# - 从 global.asax 连接到 default.aspx 上的函数

c# - 不包含定义或扩展方法 - 当它包含时

java - 如何在 Java SAX 中解析此 XML?

尝试更新 XML 中的值时出现 javax.xml.xpath.XPathExpressionException