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>

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks it kind of make sense to me, but I still not sure what to do.
I just want the same things after Foo like always, but nothing after ReportHeader and Group :)
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)
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. :)
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: stackoverflow.com/questions/1181888/what-does-xmlns-in-xml-mean/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.