簡體   English   中英

XML 文檔 (0, 0) 中存在錯誤 - 將 XML 反序列化為 object 時出錯

[英]There is an error in XML document (0, 0) - Error Deserializing XML to object

請建議我如何將 map 響應轉換為 class 因為從我遇到的每個示例中,身體總是沒有像core:transactionResponse這樣的符號

我的代碼:

string fileName = @"C:\Users\Lenovo\Downloads\GLResponseXml.xml";
        XDocument xDoc = XDocument.Load(fileName);

        var unwrappedResponse = xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
                                .First()
                                .FirstNode;

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(TransactionResponse));
        TransactionResponse response = (TransactionResponse)xmlSerializer.Deserialize(xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
            .First()
            .FirstNode
            .CreateReader()
        );

為了取消這個 soap xml:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dpss0="bons">
    <soapenv:Header/>
    <soapenv:Body>
        <core:transactionResponse xmlns:bo="http://service.example.co.id/core/bo" xmlns:core="http://service.example.co.id/core">
            <response>
                <header>
                    <coreJournal>149326</coreJournal>
                </header>
                <content xsi:type="bo:OKMessage">
                    <message/>
                </content>
            </response>
        </core:transactionResponse>
    </soapenv:Body>
   </soapenv:Envelope>

但我收到此錯誤: InvalidOperationException: <transactionResponse xmlns='http://service.example.co.id/core'> was not expected.

我正在將響應映射到這個 class:

public class GLXmlResponse
{
    [XmlRoot(ElementName = "response")]
    public class Response
    {
        [XmlElement(ElementName = "header")]
        public Header Header { get; set; }
        [XmlElement(ElementName = "content")]
        public Content Content { get; set; }
    }

    [XmlRoot(ElementName = "header")]
    public class Header
    {
        [XmlElement(ElementName = "coreJournal")]
        public string CoreJournal { get; set; }
    }

    [XmlRoot(ElementName = "content")]
    public class Content
    {
        [XmlElement(ElementName = "message")]
        public string Message { get; set; }
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
    }

    [XmlRoot(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
    public class TransactionResponse
    {
        [XmlElement(ElementName = "response")]
        public Response Response { get; set; }
        [XmlAttribute(AttributeName = "bo", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Bo { get; set; }
        [XmlAttribute(AttributeName = "core", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Core { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
        public TransactionResponse TransactionResponse { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenv { get; set; }
        [XmlAttribute(AttributeName = "soapenc", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenc { get; set; }
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "dpss0", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Dpss0 { get; set; }
    }
}

需要幫助獲得 class object 正確

我無法重現您所看到的確切錯誤,因此您可能希望從最小的重現中進行檢查; 我在用着:

var env = (GLXmlResponse.Envelope)new XmlSerializer(typeof(GLXmlResponse.Envelope))
    .Deserialize(new StringReader(xml));        
System.Console.WriteLine(env.Body.TransactionResponse.Response.Header.CoreJournal);

在哪里:

string xml = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:dpss0=""bons"">
<soapenv:Header/>
<soapenv:Body>
    <core:transactionResponse xmlns:bo=""http://service.example.co.id/core/bo"" xmlns:core=""http://service.example.co.id/core"">
        <response>
            <header>
                <coreJournal>149326</coreJournal>
            </header>
            <content xsi:type=""bo:OKMessage"">
                <message/>
            </content>
        </response>
    </core:transactionResponse>
</soapenv:Body></soapenv:Envelope>";

它只有一個null用於.Response 原因這實際上是在空命名空間中(前綴不是繼承的),所以你需要

[XmlElement(ElementName = "response", Namespace = "")]
public Response Response { get; set; }

但是,這會導致OKMessage中的 OKMessage 出現問題。如果我注釋掉<content> ,那么它會起作用,我可以看到 output 149326

但是,開始時可能更簡單,只需將 xml 復制到剪貼板,編輯 -> 選擇性粘貼 -> 將 XML 作為類粘貼,然后查看生成的內容,然后從那里開始工作。

暫無
暫無

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

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