我设置了命名空间,但我没有得到正确的 xml



我有以下代码:

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

但是结果是:

<?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>

我得到xmlns:p3和p3。

谢谢。

问题是,我在属性中使用了"ns"我只需要在XElement中使用它,而不是在XAttribute中使用它

最新更新