如何在 C# 中创建具有命名空间的文档

在有的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" />

结束语

感兴趣的读者细品。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值