tags:

views:

54

answers:

3

I want create an xml document with the namepace attributes along the lines of this:

<MyXmlDoC xmlns="http://abc" xmlns:brk="http://123"&gt;

Using the System.Xml.Linq xml library, iv done this:

     public static XAttribute XmlNamepace()
        {
            return new XAttribute(XName.Get("xmlns"), "http://abc");
        }

        public static XAttribute brkNamepace()
        {
            return new XAttribute(XNamespace.Xmlns + "brk", "http://123");
        }

 var rootNode = new XElement("MyXmlDoC",XmlNamepace(),brkNamepace());

But this produces this error:

The prefix '' cannot be redefined from '' to 'http://abc' within the same start element tag

What am I doing wrong

+2  A: 

Check this page out:

Create a Document with Namespaces (C#) (LINQ to XML)

dommer
A: 

Ok I tried this:

XNamespace rt = "http://abc";
var rootNode = new XElement(rt+"MyRootNode");
rootNode.Add(new XAttribute(XNamespace.Xmlns + "brk","http://123"))
rootNode.Add(new XElement("ChildNode", "Hello"));

This produces xml like this:

<MyRootNode xmlns:brk="http://123" xmlns="http://abc"&gt;
<Child xmlns="">Hello</Child>
</MyRootNode>

Why am I getting this extraneous xmlns="" in the Child node?

Dan
+1  A: 

(From your answer) you need to add the ChildNode with the rt namespace, this should work:

rootNode.Add(new XElement(rt + "ChildNode", "Hello"));
Si
Thanks that did the trick, you deserve an accepted answer, but this techically doesnt answer my orginal question
Dan
I think it's the same issue, you're not setting the namespace correctly.
Si