在有的XML 文档中的tag 和属性是带有分号的,例如xmlns:ua ,ua:root 等。这涉及不同的命名空间。在C#中需要特别的编程。网络上讲的稀里糊涂。这里记录了一些方法。
例1
// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
产生的XML 文档
<Root xmlns="http://www.adventure-works.com">
<Child>child content</Child>
</Root>
例2
// Create an XML tree in a namespace, with a specified prefix
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
产生的XML
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
例3:
// The http://www.adventure-works.com namespace is forced to be the default namespace.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute("xmlns", "http://www.adventure-works.com"),
new XAttribute(XNamespace.Xmlns + "fc", "www.fourthcoffee.com"),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content"),
new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
产生的XML
<Root xmlns="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<DifferentChild>other content</DifferentChild>
</fc:Child>
<Child2>c2 content</Child2>
<fc:Child3>c3 content</fc:Child3>
</Root>
例4
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", aw.NamespaceName),
new XAttribute(XNamespace.Xmlns + "fc", fc.NamespaceName),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content"),
new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
产生的XML
<aw:Root xmlns:aw="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<aw:DifferentChild>other content</aw:DifferentChild>
</fc:Child>
<aw:Child2>c2 content</aw:Child2>
<fc:Child3>c3 content</fc:Child3>
</aw:Root>
例5
// Create an XML tree in a namespace, with a specified prefix
XElement root = new XElement("{http://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement("{http://www.adventure-works.com}Child", "child content")
);
Console.WriteLine(root);
产生的XML
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
我的代码
var doc = new XDocument();
doc.Declaration = new XDeclaration(new XDeclaration("1.0", "utf-8", ""));
XNamespace aw = "http://opcfoundation.org/UA/2011/03/UANodeSet.xsd";
XElement UANodeSet = new XElement(aw+"UANodeSet");
UANodeSet.Add(new XAttribute(XNamespace.Xmlns+"xsi", "http://www.w3.org/2001/XMLSchema-instance"));
UANodeSet.Add(new XAttribute(XNamespace.Xmlns+"uax", "http://opcfoundation.org/UA/2008/02/Types.xsd"));
UANodeSet.Add(new XAttribute( "xmlns", "http://opcfoundation.org/UA/2011/03/UANodeSet.xsd"));
UANodeSet.Add(new XAttribute(XNamespace.Xmlns + "s1", "http://yourorganisation.org/demo2022/Types.xsd"));
UANodeSet.Add(new XAttribute(XNamespace.Xmlns+"ua", "http://unifiedautomation.com/Configuration/NodeSet.xsd"));
UANodeSet.Add(new XAttribute(XNamespace.Xmlns+"xsd", "http://www.w3.org/2001/XMLSchema")); doc.Add(UANodeSet);
doc.Save(FileName);
产生的XML 文档
<?xml version="1.0" encoding="utf-8"?>
<UANodeSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" xmlns:s1="http://yourorganisation.org/demo2022/Types.xsd" xmlns:ua="http://unifiedautomation.com/Configuration/NodeSet.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
结束语
感兴趣的读者细品。