Krzysztof Kaczor
Krzysztof Kaczor

Reputation: 5588

Creating namespace for XML element

I have problem with adding new node (with namespace) to my xml document which is used to generating xaml. I'm doing it like this:

XmlElement richTextColumns = xmlDoc.CreateElement("local2:RichTextColumns");

but i receive error 0xC00CE01D (while calling xmlDoc.getxml). I have tried adding attribute xmlns:local2="using:App2.Common" to xmlDoc:

var att = xmlDoc.CreateAttribute("xmlns:local2");
att.InnerText = "using:Dictionary.Common";
xmlDoc.Attributes.SetNamedItem(att);

it results in this error

Object reference not set to an instance of an object.

Thank you in advance :)

Upvotes: 0

Views: 3271

Answers (3)

COLD TOLD
COLD TOLD

Reputation: 13579

You can create an element as you would typically do and then load the document back and add the namespace atribute you are looking to add.

  XmlDocument doc = new XmlDocument();
    doc.LoadXml("link to yuor xml");
    XNamespace xmlns = "local2";
    public static void SetDefaultXmlNamespace(XElement xelem, XNamespace xmlns)
    {

        foreach(var e in xelem.Elements())
            e.SetDefaultXmlNamespace(xmlns);
    }

    doc.Root.SetDefaultXmlNamespace("local2")

Upvotes: 1

Dan
Dan

Reputation: 9837

If you'd like to create an element with a specific namespace use this call:

xmlDoc.CreateElementNS("using:Dictionary.Common", "local2:elementName")

Upvotes: 1

Kevin Mangold
Kevin Mangold

Reputation: 1155

According to http://msdn.microsoft.com/en-us/library/aa335908(v=vs.71), the CreateAttribute method with a single parameter does not set the namespace, but the name of the element. Try to use one of the other permutations of this method.

Upvotes: 1

Related Questions