1

When I try to parse XDocument from text, I can get the default namespace like this:

var xmlDocument1 = XDocument.Parse("<root xmlns='http://somenamespace'></root>");
var xmlNamespace1 = xmlDocument1.Root.GetDefaultNamespace().NamespaceName; // somenamespace

However, if I try to create XDocument manually, I'm getting an empty value:

var xmlRoot2 = new XElement(XName.Get("root", "http://somenamespace"));
var xmlDocument2 = new XDocument(xmlRoot2);
var xmlNamespace2 = xmlDocument2.Root.GetDefaultNamespace().NamespaceName; // is empty

I expect xmlNamespace2 to be "http://somenamespace". Is there anything I'm doing wrong?

Edit: The answer in suggested duplicate doesn't solve my problem, even if I use the function suggested here: How to set the default XML namespace for an XDocument I will still get the empty namespace. Here is the solution from the linked question:

class Program
{
    static void Main(string[] args)
    {
        var xmlRoot = new XElement(XName.Get("root"));
        var xmlDocument = new XDocument(xmlRoot);
        SetDefaultXmlNamespace(xmlRoot, "http://somenamespace");
        var xmlNamespace = xmlDocument.Root.GetDefaultNamespace().NamespaceName; // is empty
    }

    public static void SetDefaultXmlNamespace(XElement xelem, XNamespace xmlns)
    {
        if (xelem.Name.NamespaceName == string.Empty)
            xelem.Name = xmlns + xelem.Name.LocalName;
        foreach (var e in xelem.Elements())
            SetDefaultXmlNamespace(e, xmlns);
    }
}

The default namespace is still empty.

Community
  • 1
  • 1
username
  • 3,378
  • 5
  • 44
  • 75
  • possible duplicate of [How to set the default XML namespace for an XDocument](http://stackoverflow.com/questions/2874422/how-to-set-the-default-xml-namespace-for-an-xdocument) – MethodMan May 27 '15 at 17:36

2 Answers2

4

I think I understand why I don't get the default namespace. According to MSDN:

When creating XML trees using C#, even if an XML tree would be serialized with a default namespace, if the namespace is not persisted in the XML tree as an attribute, this method will not report the namespace as the default namespace.

So I probably need to set the xmlns attribute explicitly:

class Program
{
    static void Main(string[] args)
    {
        var xmlRoot = new XElement(XName.Get("root", "http://somenamespace"));
        xmlRoot.SetAttributeValue("xmlns", "http://somenamespace");
        var xmlDocument = new XDocument(xmlRoot);
        var xmlNamespace = xmlDocument.Root.GetDefaultNamespace().NamespaceName;
    }
}

Now I'm getting the correct namespace name.

username
  • 3,378
  • 5
  • 44
  • 75
  • 1
    Yes, this is about the easiest way. I was looking for something cleverer but that didn't work out. – H H May 27 '15 at 18:04
1

Yes, tricky because even as the string representation looks OK it still doesn't work.

For education, add this:

var xml3 = xmlDocument2.ToString();
var xmlDocument3 = XDocument.Parse(xml3);            
var xmlNamespace3 = xmlDocument3.Root.GetDefaultNamespace().NamespaceName;

So only after you Parse the xml text again it is what you would expect.

The probably shortest way to do this:

var xmlRoot2 = new XElement(XName.Get("root", "http://somenamespace"), 
       new XAttribute("xmlns", "http://somenamespace"));

Using the predefined XNameSpace.Xmlns doesn't work for the default (no prefix) namespace, so just "xmlns" has to do.

H H
  • 263,252
  • 30
  • 330
  • 514