簡體   English   中英

XDocument錯誤名稱不能以'<'字符開頭,十六進制值0x3C

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

我正在嘗試使用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生成的示例XML文件 在此輸入圖像描述

有一種更簡單的方法可以做到這一點,而不是通過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: attributes)。 其次,你在調用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>

你正在做的是一個非常難的方法。 有一種更簡單的方法。

您有來自它們的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個對象,包括所有枚舉,以及生成符合其規范的數據所必需的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM