asdasdad
asdasdad

Reputation: 862

How do I change the xmlns in a new StartElement in C# with XMLWriter?

I need the following result:

<?xml version="1.0" encoding="utf-8"?>
<LIEFERUNG-AUSWI xmlns:aw="http://www.bundesbank.de/xmw/auswi/2003-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/2003-01-01 BbkXmwAuswi.xsd" version="1.0" erstellzeit="2013-11-06T15:36:44" stufe="Test" xmlns="http://www.bundesbank.de/xmw/auswi/2003-01-01">
  <ABSENDER xmlns="http://www.bundesbank.de/xmw/2003-01-01">
    <aw:FIRMENNR>00004711</aw:FIRMENNR>
...

My code is the following:

writer.WriteStartElement("LIEFERUNG-AUSWI", "http://www.bundesbank.de/xmw/auswi/2003-01-01");
writer.WriteAttributeString("xmlns", "aw", null, "http://www.bundesbank.de/xmw/auswi/2003-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/2003-01-01 BbkXmwAuswi.xsd");
writer.WriteAttributeString(null, "version", null, "1.0");
writer.WriteAttributeString(null, "erstellzeit", null, Dat_DatZeit);
writer.WriteAttributeString(null, "stufe", null, "Test");

// Start-Tag von Absender
writer.WriteStartElement("ABSENDER");
writer.WriteAttributeString("xmlns", "bbk", null, "http://www.bundesbank.de/xmw/2003-01-01");
writer.WriteElementString("aw", "FIRMENNR", null, "00004711");

But this gives me the following:

<?xml version="1.0" encoding="utf-8"?>
<LIEFERUNG-AUSWI xmlns:aw="http://www.bundesbank.de/xmw/auswi/2003-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/2003-01-01 BbkXmwAuswi.xsd" version="1.0" erstellzeit="2013-11-06T15:36:44" stufe="Test" xmlns="http://www.bundesbank.de/xmw/auswi/2003-01-01">
  <ABSENDER xmlns:bbk="http://www.bundesbank.de/xmw/2003-01-01">
    <aw:FIRMENNR>00004711</aw:FIRMENNR>

What do I have to change to make xmlns:bbk become only xmlns? I never did something in XML so I am actually just trying but all I tried didn't work, either I got an error message or the result was wrong... here is something different I tried:

// Start-Tag von Absender
writer.WriteStartElement("ABSENDER", "http://www.bundesbank.de/xmw/2003-01-01");

This gave me a wrong result...

Upvotes: 0

Views: 339

Answers (1)

madd0
madd0

Reputation: 9323

You have to change the following line:

writer.WriteStartElement("ABSENDER");

to:

writer.WriteStartElement("", "ABSENDER","http://www.bundesbank.de/xmw/2003-01-01");

The next one is unnecessary.

I may add that your XML seems a little overcomplicated, with namespaces being often redefined. For example in you root tag, http://www.bundesbank.de/xmw/auswi/2003-01-01 is the default namespace, but it is also called aw, and the namespace that you called bbk is later used, without its name… Maybe you should consider simplifying your XML in the process.

Aside from that, not testing your code before posting will often get your question closed on SO. I recommend LinqPad for this. It's what I used to find your answer.

Upvotes: 2

Related Questions