简体   繁体   English

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

[英]XDocument Error Name cannot begin with the '<' character, hexadecimal value 0x3C

I am trying to create xml file using XDcoument, but I am getting following error 我正在尝试使用XDcoument创建xml文件,但我收到以下错误

Name cannot begin with the '<' character, hexadecimal value 0x3C 名称不能以'<'字符开头,十六进制值0x3C

here is my code 这是我的代码

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

Can someone please help me on this? 有人可以帮我这个吗?

here is sample XML file which I want to generate using XDocument 这是我想用XDocument生成的示例XML文件 在此输入图像描述

There is a much simpler way to do this rather than crafting the XML document by hand via XDocument , though I have an explanation and example below if you want to do it that way. 有一种更简单的方法可以做到这一点,而不是通过XDocument手工制作XML文档,尽管如果你想这样做,我有一个解释和示例。

First, the simple way - create the XML as a string , and pass that string to XDocument.Parse , like this: 首先,简单的方法 - 将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 will contain the XML you wish to send. xDoc2将包含您要发送的XML。

If you wish to do it the long way, then there are a couple of issues with your posted code. 如果您希望这么做,那么您发布的代码会有一些问题。

First, you're not correctly handling the namespaces (the xmlns: attributes). 首先,您没有正确处理命名空间( xmlns: attributes)。 Secondly, you're including the < and > in the call to XElement , and you don't need to do that - the method takes care of those two symbols. 其次,你在调用XElement包含<> ,而你不需要这样做 - 该方法处理这两个符号。

What you need to do is to set up the namespaces, then add them to the appropriate elements as well as creating the attributes for them. 您需要做的是设置命名空间,然后将它们添加到适当的元素以及为它们创建属性。

The sample code doesn't match the posted snippet, so I worked off your sample code to show you how to go about crafting the XML by hand. 示例代码与发布的代码段不匹配,因此我设计了示例代码,向您展示如何手动制作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")
                        ))
    );

The first thing the above code does is sets up all the namespaces via XNamespace . 上面代码所做的第一件事是通过XNamespace设置所有命名空间。

Next, the XML Document is constructed. 接下来,构建XML文档。 The individual elements are created via XElement , with the various namespaces prefixed (ie, new XElement(sNS + "Envelope", , and the other namespaces added via XAttribute . 各个元素是通过XElement创建的,各种名称空间都带有前缀(即new XElement(sNS + "Envelope", ,以及通过XAttribute添加的其他名称空间。

Nesting can get tricky, so you have to be very careful doing it this way. 嵌套可能会变得棘手,所以你必须非常小心这样做。 The above code will produce the following XML: 上面的代码将生成以下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>

What you're doing is a really hard way to do this. 你正在做的是一个非常难的方法。 There is a much easier way. 有一种更简单的方法。

You have the Xsd specifications from them, you can use the xsd command in the Visual Studio command line to generate C# objects that match the requirements automatically during serialization. 您有来自它们的Xsd规范,您可以使用Visual Studio命令行中的xsd命令生成在序列化期间自动匹配要求的C#对象。

For the IRS ACA schemas, get all the XSD files into the same directory. 对于IRS ACA模式,将所有XSD文件放入同一目录中。 Then in a sibling directory to the one you created, place the Common folder. 然后在兄弟目录中创建您创建的目录,放置Common文件夹。

Then, in the command line navigate to the directory you created and put all the xsd files and run this command: 然后,在命令行中导航到您创建的目录并放入所有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

You'll end up with a C# file that has almost 200 objects in it including all the enums and such necessary for generating data compliant with their specifications. 最终会得到一个C#文件,其中包含近200个对象,包括所有枚举,以及生成符合其规范的数据所必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称不能以 '&lt;' 字符十六进制值 0x3c 开头 - Name cannot begin with the '<' character hexadecimal value 0x3c WCF服务参考 - 获取“XmlException:名称不能以&#39;&lt;&#39;字符开头,十六进制值0x3C”在客户端 - WCF Service Reference - Getting “XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side &#39;&lt;&#39;,十六进制值 0x3C,是无效的属性字符 - '<', hexadecimal value 0x3C, is an invalid attribute character DataContractSerializer-名称不能以“。”开头 字符,十六进制值0x2E - DataContractSerializer - Name cannot begin with the '.' character, hexadecimal value 0x2E 名称不能以&#39;&#39;字符开头,十六进制值0x20 - Name cannot begin with the ' ' character, hexadecimal value 0x20 名称不能以“1”字符开头,十六进制值0x31。 从xml文件中读取时 - Name cannot begin with the '1' character, hexadecimal value 0x31. while reading from an xml file 将Firebase广告添加到Xamarin.Forms会导致:名称不能以&#39;$&#39;字符开头,十六进制值为0x24 - Adding Firebase Ads to Xamarin.Forms causes: Name cannot begin with the '$' character, hexadecimal value 0x24 使用xslt时,名称不能以&#39;=&#39;字符十六进制值0x3d开头 - Name cannot begin with the '=' character hexadecimal value 0x3d when use xslt xml.Linq:“名称不能以 &#39;?&#39; 开头字符,十六进制值 0x3F&quot; - xml.Linq: "Name cannot begin with the '?' character, hexadecimal value 0x3F" 名称不能以“1”字符开头,十六进制值0x31。 第2行,第2位 - Name cannot begin with the '1' character, hexadecimal value 0x31. Line 2, position 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM