0

I have this

XNamespace ns = "http://something0.com";
XNamespace xsi = "http://something1.com";
XNamespace schemaLocation = "http://something3.com";

XDocument doc2 = new XDocument(
    new XElement(ns.GetName("Foo"),
        new XAttribute(XNamespace.Xmlns + "xsi", xsi),
        new XAttribute(xsi.GetName("schemaLocation"), schemaLocation),
        new XElement("ReportHeader", GetSection()),
        GetGroup() 
    )
);

It gives

<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:xsi="http://something1.com"
xsi:schemaLocation="http://something3.com" 
xmlns="http://something0.com">
    <ReportHeader xmlns="">
        ...
    </ReportHeader>
    <Group xmlns="">
        ...
    </Group>
</Foo>

But I wan't this result, how can it be done? (Notice the xmlns=""is missing..)

<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:xsi="http://something1.com"
xsi:schemaLocation="http://something3.com" 
xmlns="http://something0.com">
    <ReportHeader>
        ...
    </ReportHeader>
    <Group>
        ...
    </Group>
</Foo>
pnuts
  • 58,317
  • 11
  • 87
  • 139
radbyx
  • 9,352
  • 21
  • 84
  • 127

1 Answers1

3

Your problem here is that you are setting the default namespace for the document to "http://something0.com", but then appending elements that are not in this namespace - they are in the empty namespace.

Your document states it has a default namespace of xmlns="http://something0.com", but then you append elements which are in the empty namespace (because you didn't supply their namespace when you appended them) - so they are all getting explicitly marked with xmlns='' to show they are not in the default namespace of the document.

This means there are two solutions to getting rid of the xmlns="", but they have different meanings:

1) If you mean you definitely want the xmlns="http://something0.com" at the root element (specifying the default namespace for the document) - then to "disappear" the xmlns="" you need to you need to supply this namespace when creating the elements:

// create a ReportHeader element in the namespace http://something0.com
new XElement(ns + "ReportHeader", GetSection())

2) If these elements are not meant to be in the namespace "http://something0.com", then you mustn't add it as the default at the top of the document (the xmlns="http://something0.com" bit on the root element).

XDocument doc2 = new XDocument(
     new XElement("foo",  // note - just the element name, rather  s.GetName("Foo")
          new XAttribute(XNamespace.Xmlns + "xsi", xsi),

The sample output you expect, suggests the former of these two choices.

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
  • Thanks it kind of make sense to me, but I still not sure what to do. – radbyx Apr 18 '12 at 08:31
  • I just want the same things after `Foo` like always, but nothing after `ReportHeader` and `Group` :) – radbyx Apr 18 '12 at 08:37
  • If I replace `ns.GetName("Foo")`with `Foo`, I don't get the `xmlns="http://something0.com"` after `Foo`, if that makes sense. (it removes to much) – radbyx Apr 18 '12 at 08:40
  • I see you're putting alot of effort to the answer and alot of edits. I trying to keep up. I'll need to read it a couple of times for it to sink in and then test it. :) – radbyx Apr 18 '12 at 08:48
  • After reading "they are in the empty namespace." I got an idea: I added `ns + ` to `Report` : (new XElement(ns + "ReportHeader", GetSection()),) And know `xmlns=""`is gone :) – radbyx Apr 18 '12 at 08:55
  • 1
    Cool - does this mean your problem is solved now? The thing to understand here is _what_ the namespace is actually saying about the document and its element; once this has clicked, the behaviours you are seeing will make sense (and how to solve them). Have a look at this previous answer - it should make it all clear if it isn't already: http://stackoverflow.com/questions/1181888/what-does-xmlns-in-xml-mean/1181936#1181936 – Rob Levine Apr 18 '12 at 09:08
  • I went with your (1) solution. I have added "ns + " to all my subelements now (about 25ish) and it works perfectly now - Thanks! Now I have to be carefull to remember to add the `ns + ` everytime I later add some elements. It concern me a bit, because I could easily make a silent error. – radbyx Apr 18 '12 at 09:20