简体   繁体   中英

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

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.

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

如果要创建具有特定名称空间的元素,请使用以下调用

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM