我有以下代码:
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。
谢谢。