2

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

The output is this:

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

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

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

Community
  • 1
  • 1
asdasdad
  • 832
  • 3
  • 15
  • 27

2 Answers2

2

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

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.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
0

Have you tried:

 writer.WriteStartElement("LIEFERUNG-AUSWI");      
 writer.WriteAttributeString("xmlns", "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");
K893824
  • 1,279
  • 8
  • 21
  • 1
    I tried that before but it didn't work i got something different as output... I can try it again but i think it's not the right answer – asdasdad Dec 05 '13 at 14:19
  • This returns an XmlException. @Jan-PeterVos explains [here](http://stackoverflow.com/questions/6048695/xml-writeattributestring-error) that "you need to define the namespace of the element on the WriteStartElement itself." His answer corrects the exception, but doesn't solve the order issue. –  Feb 08 '17 at 20:37