c#中的复杂Xml命名空间

时间:2015-09-02 16:21:32

标签: c# xml linq linq-to-xml

我正在尝试在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");

2 个答案:

答案 0 :(得分:3)

您可能知道,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)
            )
        )
    );

答案 1 :(得分:0)

这就是我通常的做法

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

        }
    }
}
​