c#中复杂的Xml名称空间

本文关键字:空间 Xml 复杂 | 更新日期: 2023-09-27 18:10:57

我试图在c#中创建一个具有多个名称空间的xml。我使用LINQ XDocument对象来创建xml。xml的多个名称空间引起了很多混淆。有谁能告诉我正确的方向吗?

<?xml version="1.0" encoding="UTF-8"?>
<n1:Form109495CTransmittalUpstream
xmlns="urn:us:gov:treasury:irs:ext:aca:air:6.2" xmlns:irs="urn:us:gov:treasury:irs:common"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:n1="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
xsi:schemaLocation="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage IRS-Form1094-1095CTransmitterUpstreamMessage.xsd">
<Form1094CUpstreamDetail recordType="String" lineNum="0">
    <SubmissionId>1</SubmissionId>
    <irs:TaxYr>1000</irs:TaxYr>
    <irs:CorrectedInd>true</irs:CorrectedInd>
 </Form1094CUpstreamDetail>
</n1:Form109495CTransmittalUpstream>

c#代码
            new XDocument(
            new XAttribute(XNamespace.Xmlns + "version", "1.0"),
            new XAttribute(XNamespace.Xmlns + "encoding", "UTF-8"),
                new XElement("n1:Form109495CTransmittalUpstream","")
            ).Save("sample.xml");

c#中复杂的Xml名称空间

您可能已经注意到,XML中的"限定名称"由名称空间和本地名称组成。为了使其更容易处理,您可以在这些名称空间的前缀中包含名称空间声明。

因此,例如,在您看到irs:TaxYr的地方,限定名实际上是由urn:us:gov:treasury:irs:common名称空间和TaxYr本地名称组成的。

前缀值本身并不重要——它们只是一种查找机制。LINQ to XML将自动为您处理这个问题(通过生成唯一的前缀,通常是p1p2等),但是您可以自己将它们包含为属性。要为irs名称空间创建这样的声明,您需要创建以下属性:

new XAttribute(XNamespace.Xmlns + "irs", "urn:us:gov:treasury:irs:common")

LINQ to XML还提供了一些漂亮的隐式转换,允许您从字符串隐式地创建XNamespace和合格的XName。因此,要获得TaxYr名称,您需要执行以下操作:

XNamespace irs = "urn:us:gov:treasury:irs:common";
XName taxYr = irs + "TaxYr";

对于您的每个元素和属性,您的XML可以像这样声明式地创建:

XNamespace def = "urn:us:gov:treasury:irs:ext:aca:air:6.2";
XNamespace irs = "urn:us:gov:treasury:irs:common";
XNamespace n1 = "urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
var schemaHint = "urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage IRS-Form1094-1095CTransmitterUpstreamMessage.xsd";
var doc = new XDocument(
    new XElement(n1 + "Form109495CTransmittalUpstream",
        new XAttribute("xmlns", def),
        new XAttribute(XNamespace.Xmlns + "irs", irs),
        new XAttribute(XNamespace.Xmlns + "xsi", xsi),
        new XAttribute(XNamespace.Xmlns + "n1", n1),
        new XAttribute(xsi + "schemaLocation", schemaHint),
        new XElement(def + "Form1094CUpstreamDetail",
            new XAttribute("recordType", "String"),
            new XAttribute("lineNum", 0),
            new XElement(def + "SubmissionId", 1),
            new XElement(irs + "TaxYr", 1000),
            new XElement(irs + "CorrectedInd", true)
            )
        )
    );

我通常是这样做的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
              "<?xml version='"1.0'" encoding='"UTF-8'"?>" +
                "<n1:Form109495CTransmittalUpstream" +
                    " xmlns='"urn:us:gov:treasury:irs:ext:aca:air:6.2'" xmlns:irs='"urn:us:gov:treasury:irs:common'"" +
                    " xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance'"" +
                    " xmlns:n1='"urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage'"" +
                    " xsi:schemaLocation='"urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage IRS-Form1094-1095CTransmitterUpstreamMessage.xsd'">" +
                "</n1:Form109495CTransmittalUpstream>";
            XDocument doc = XDocument.Parse(xml);
            XElement form109495CTransmittalUpstream = (XElement)doc.FirstNode;
            XNamespace  def = form109495CTransmittalUpstream.GetDefaultNamespace();
            XNamespace irs = form109495CTransmittalUpstream.GetNamespaceOfPrefix("irs");
            XNamespace n1 = form109495CTransmittalUpstream.GetNamespaceOfPrefix("n1");
            XElement form1094CUpstreamDetail = new XElement(def + "Form1094CUpstreamDetail", new XAttribute[] {
                new XAttribute("recordType", "String"), new XAttribute("lineNum", 0)
            });
            form109495CTransmittalUpstream.Add(form1094CUpstreamDetail);
            form109495CTransmittalUpstream.Add(new XElement("SubmissionId", 1));
            form109495CTransmittalUpstream.Add(new XElement(irs + "TaxYr", 1000));
            form109495CTransmittalUpstream.Add(new XElement(irs + "CorrectedInd", true));
        }
    }
}
​