0

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 :)

3 Answers 3

1

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

xmlDoc.CreateElementNS("using:Dictionary.Common", "local2:elementName")
Sign up to request clarification or add additional context in comments.

4 Comments

In win8 there is no xmlDoc.CreateElement that takes 3 args :/
What XML API are you using? I assumed you were using System.Xml.XmlDocument.
"there is no System.Xml.XmlDocument in system.xml" :/
I'm not familiar with this API, but it looks like you can use the CreateElementNS method, supplying "using:Dictionary.Common" and "local2:elementName" as parameters. I'll update the answer.
1

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.

1 Comment

there is only function createElement with one arg in windows8
1

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")

1 Comment

Could you write something more? I don't know how to access doc.Root element.

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.