简体   繁体   中英

I set the namespace but I I don't get the correct xml

I have the following code:

XNamespace xsiNs = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
            XNamespace ns = XNamespace.Get("http://xyz.com/2006/");
            XDocument doc = new XDocument(
                new XDeclaration("1.0", "UTF-8", null),
                new XElement(ns + "Node",
                    new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
                    new XAttribute(xsiNs + "schemaLocation",
                        "http://aaa.com/bbb.xsd")
            ));


            XElement newElement = new XElement(ns + "PrincipalNode",
                new XAttribute(ns + "Attributte01", "value Attributte01"),
                new XAttribute(ns + "Attributte02", "Value Attributte02"),
                new XElement(ns + "SubNode01", " value SubNode01"),
                new XElement(ns + "SubNode02", " value SubNode02"));

            doc.Root.Add(newElement);

But the result is:

<?xml version="1.0" encoding="utf-8"?>
<Node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://aaa.com/bbb.xsd" xmlns="http://aaa.com/bbb">
  <PrincipalNode p3:Attributte01="value Attributte01" p3:Attributte02="Value Attributte02" xmlns:p3="http://aaa.com/bbb">
    <p3:SubNode01> value SubNode01</p3:SubNode01>
    <p3:SubNode02> value SubNode02</p3:SubNode02>
  </PrincipalNode>
</Node>

I get xmlns:p3 and p3.

Thanks.

好吧,问题在于我在属性中使用了“ ns”,而我只需要在XElement中使用它,而在XAttribute中则不需要。

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