XDocument错误名称不能以'<'开头字符,十六进制值0x3C

时间:2016-02-02 05:52:50

标签: c# xml

我正在尝试使用XDcoument创建xml文件,但我收到了以下错误

  

名称不能以'<'开头字符,十六进制值0x3C

这是我的代码

XDocument d = new XDocument(
                new XElement("<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>",
                    new XElement("<S:Header xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>",
                        new XElement("<ns13:ACASecurityHeader xmlns='urn:us:gov:treasury:irs:ext:aca:air:7.0' xmlns:ns10='urn:us:gov:treasury:irs:msg:acauibusinessheader' xmlns:ns11='http://www.w3.org/2000/09/xmldsig#' xmlns:ns12='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd' xmlns:ns13='urn:us:gov:treasury:irs:msg:acasecurityheader' xmlns:ns2='urn:us:gov:treasury:irs:common' xmlns:ns3='urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage' xmlns:ns4='urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage' xmlns:ns5='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd' xmlns:ns6='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage' xmlns:ns7='urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage' xmlns:ns8='urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter' xmlns:ns9='urn:us:gov:treasury:irs:msg:acabusinessheader'>"),
                        new XElement("Author", "Moreno, Jordao")
                        ),
                        new XElement("Book",
                        new XElement("Title", "Midieval Tools and Implements"),
                        new XElement("Author", "Gazit, Inbar")
                        )
                    ),
                new XComment("This is another comment."));

有人可以帮我这个吗?

这里是我想使用XDocument enter image description here

生成的示例XML文件

2 个答案:

答案 0 :(得分:3)

有一种更简单的方法可以做到这一点,而不是通过XDocument手工制作XML文档,但如果你想这样做,我会在下面给出解释和示例。

首先,简单的方法 - 将XML创建为string,并将该字符串传递给XDocument.Parse,如下所示:

string xmlString = @"<S:Envelope xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/""><S:Header xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""><ns13:ACASecurityHeader xmlns:ns10=""urn:us:gov:treasury:irs:msg:acauibusinessheader"" xmlns:ns11=""http://www.w3.org/2000/09/xmldsig#"" xmlns:ns12=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"" xmlns:ns13=""urn:us:gov:treasury:irs:msg:acasecurityheader"" xmlns:ns2=""urn:us:gov:treasury:irs:common"" xmlns:ns3=""urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage"" xmlns:ns4=""urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"" xmlns:ns5=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"" xmlns:ns6=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage"" xmlns:ns7=""urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"" xmlns:ns8=""urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter"" xmlns:ns9=""urn:us:gov:treasury:irs:msg:acabusinessheader""><Author>Moreno, Jordao</Author><Book><Title>Midieval Tools and Implement</Title><Author>Gazit, Inbar</Author></Book></ns13:ACASecurityHeader><!--This is another comment--></S:Header></S:Envelope>";

XDocument xDoc2 = XDocument.Parse(xmlString);

xDoc2将包含您要发送的XML。

如果您希望这么做,那么您发布的代码就会出现一些问题。

首先,您没有正确处理命名空间(xmlns:属性)。其次,您在<的调用中包含>XElement,而您不需要这样做 - 该方法会处理这两个符号。

您需要做的是设置命名空间,然后将它们添加到适当的元素以及为它们创建属性。

示例代码与发布的代码段不匹配,因此我设计了示例代码,向您展示如何手动制作XML。

XNamespace sNS = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
XNamespace wsseNS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace xmlnsNS = XNamespace.Get("urn:us:gov:treasury:irs:ext:aca:air:7.0");
XNamespace ns10NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acauibusinessheader");
XNamespace ns11NS = XNamespace.Get("http://www.w3.org/2000/09/xmldsig#");
XNamespace ns12NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XNamespace ns13NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acasecurityheader");
XNamespace ns2NS = XNamespace.Get("xmlns: ns2 = 'urn:us:gov:treasury:irs:common");
XNamespace ns3NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage");
XNamespace ns4NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage");
XNamespace ns5NS = XNamespace.Get("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
XNamespace ns6NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage");
XNamespace ns7NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage");
XNamespace ns8NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter");
XNamespace ns9NS = XNamespace.Get("urn:us:gov:treasury:irs:msg:acabusinessheader");

XDocument xDoc = new XDocument(new XElement(sNS + "Envelope", new XAttribute(XNamespace.Xmlns + "S", sNS),
                        new XElement(sNS + "Header", new XAttribute(XNamespace.Xmlns + "wsse", wsseNS),
                            new XElement(ns13NS + "ACASecurityHeader", new XAttribute(XNamespace.Xmlns + "ns10", ns10NS),
                                new XAttribute(XNamespace.Xmlns + "ns11", ns11NS),
                                new XAttribute(XNamespace.Xmlns + "ns12", ns12NS),
                                new XAttribute(XNamespace.Xmlns + "ns13", ns13NS),
                                new XAttribute(XNamespace.Xmlns + "ns2", ns2NS),
                                new XAttribute(XNamespace.Xmlns + "ns3", ns3NS),
                                new XAttribute(XNamespace.Xmlns + "ns4", ns4NS),
                                new XAttribute(XNamespace.Xmlns + "ns5", ns5NS),
                                new XAttribute(XNamespace.Xmlns + "ns6", ns6NS),
                                new XAttribute(XNamespace.Xmlns + "ns7", ns7NS),
                                new XAttribute(XNamespace.Xmlns + "ns8", ns8NS),
                                new XAttribute(XNamespace.Xmlns + "ns9", ns9NS
                                new XAttribute("xmlns", xmlnsNS),
                                new XElement("Author", "Moreno, Jordao"),
                                new XElement("Book",
                                    new XElement("Title", "Midieval Tools and Implement"),
                                    new XElement("Author", "Gazit, Inbar"))
                                ),
                            new XComment("This is another comment")
                        ))
    );

上面代码所做的第一件事是通过XNamespace设置所有名称空间。

接下来,构建XML文档。单个元素是通过XElement创建的,其中各种名称空间带有前缀(即new XElement(sNS + "Envelope",,其他名称空间通过XAttribute添加。

嵌套可能会变得棘手,所以你必须非常小心这样做。上面的代码将生成以下XML:

<?xml version="1.0"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <ns13:ACASecurityHeader xmlns="urn:us:gov:treasury:irs:ext:aca:air:7.0"
                            xmlns:ns9="urn:us:gov:treasury:irs:msg:acabusinessheader"
                            xmlns:ns8="urn:us:gov:treasury:irs:msg:irsacabulkrequesttransmitter" 
                            xmlns:ns7="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmitterreqmessage"
                            xmlns:ns6="urn:us:gov:treasury:irs:msg:form1094-1095BCtransmittermessage" 
                            xmlns:ns5="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                            xmlns:ns4="urn:us:gov:treasury:irs:msg:form1094-1095Ctransmitterupstreammessage"
                            xmlns:ns3="urn:us:gov:treasury:irs:msg:form1094-1095Btransmitterupstreammessage"                                xmlns:ns2="urn:us:gov:treasury:irs:common" 
                            xmlns:ns13="urn:us:gov:treasury:irs:msg:acasecurityheader" 
                            xmlns:ns12="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                            xmlns:ns11="http://www.w3.org/2000/09/xmldsig#" 
                            xmlns:ns10="urn:us:gov:treasury:irs:msg:acauibusinessheader">
      <Author>Moreno, Jordao</Author>
      <Book>
        <Title>Midieval Tools and Implement</Title>
        <Author>Gazit, Inbar</Author>
      </Book>
    </ns13:ACASecurityHeader>
    <!--This is another comment-->
  </S:Header>
</S:Envelope>

答案 1 :(得分:0)

你正在做的事情是一个非常难的方法。有一种更简单的方法。

您有来自它们的Xsd规范,您可以使用Visual Studio命令行中的xsd命令生成在序列化期间自动匹配要求的C#对象。

对于IRS ACA架构,将所有XSD文件放入同一目录。然后在兄弟目录中创建您创建的目录,放置Common文件夹。

然后,在命令行中导航到您创建的目录并放入所有xsd文件并运行此命令:

xsd /c IRS-EXT-ACA-AIR-7.0.xsd IRS-ACABulkRequestTransmitterMessage.xsd IRS-Form1094-1095CTransmitterUpstreamMessage.xsd IRS-CAC.xsd IRS-WSTimeStampElementMessage.xsd IRS-WSTimeStampElementMessage.xsd

您最终会得到一个C#文件,其中包含近200个对象,包括所有枚举,以及生成符合其规范的数据所必需的。