asdasdad
asdasdad

Reputation: 862

C# Write xmlns before other attributes with XMLWriter

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

Upvotes: 2

Views: 6401

Answers (3)

lauxjpn
lauxjpn

Reputation: 5254

I gave a simple answer with code to a similar question, that shows how to control the order of the xmlns attribute by explicitly adding it as a regular XAttribute to an XElement (no custom XmlWriter implementation needed).

Upvotes: 0

Dirk Vollmar
Dirk Vollmar

Reputation: 176169

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.

Upvotes: 2

K893824
K893824

Reputation: 1319

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

Upvotes: 0

Related Questions