C# 使用 XMLWriter 在其他属性之前编写 xmlns [英] C# Write xmlns before other attributes with XMLWriter

查看:27
本文介绍了C# 使用 XMLWriter 在其他属性之前编写 xmlns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是制作 XML 起始元素的代码:

This is the code to make the startelement of the 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 属性成为第一个?应该是这样的:

What can I do to change the order so that the xmlns attribute is the first one? It should be like this:

<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">

这个问题是一样的,但没有答案:编写带有属性的 xmlns 元素

This Question is the same but there was no answer: Write xmlns element with attributes

推荐答案

您可以通过以下代码获得想要的结果:

You can achieve the desired result with the following code:

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´ 前缀 _or_ 编写根元素 (LIEFERUNG-AUSWI) 以删除aw` 前缀.

However, note that the aw namespace prefix refers to the same namespace as the default namespace. They both refer to "http://www.bundesbank.de/xmw/auswi/2013-01-01". A fix would be to write the root element (LIEFERUNG-AUSWI) with the aw´ prefix _or_ to remove theaw` prefix.

这篇关于C# 使用 XMLWriter 在其他属性之前编写 xmlns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆