Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML文档(0,0)中存在错误-将XML反序列化为对象时出错_C#_Xml_Xml Serialization_Xml Deserialization - Fatal编程技术网

C# XML文档(0,0)中存在错误-将XML反序列化为对象时出错

C# XML文档(0,0)中存在错误-将XML反序列化为对象时出错,c#,xml,xml-serialization,xml-deserialization,C#,Xml,Xml Serialization,Xml Deserialization,请建议我如何将响应映射到类中,因为从我遇到的每个示例中,body总是没有类似core:transactionResponse 我的代码: string fileName = @"C:\Users\Lenovo\Downloads\GLResponseXml.xml"; XDocument xDoc = XDocument.Load(fileName); var unwrappedResponse = xDoc.Descendants((XNa

请建议我如何将响应映射到类中,因为从我遇到的每个示例中,body总是没有类似
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,请执行以下操作:


149326
但是我得到了这个错误:
invalidoOperationException:不是预期的。

我正在将响应映射到这个类中:

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

需要关于正确设置类对象的帮助吗?

我无法重新设置您看到的确切错误,因此您可能需要从最小的重新设置中进行检查;我正在使用:

var env=(GLXmlResponse.Envelope)新的XmlSerializer(typeof(GLXmlResponse.Envelope))
.反序列化(新的StringReader(xml));
System.Console.WriteLine(env.Body.TransactionResponse.Response.Header.CoreJournal);
其中:

stringxml=@”
149326
";
对于
.Response
,它只有一个
null
。原因是这实际上是在空名称空间中(前缀不是继承的),因此需要

[xmlement(ElementName=“response”,Namespace=”“)]
公共响应{get;set;}
但是,这会导致xml中的
OKMessage
出现问题。如果我注释掉
,那么它会工作,我可以看到输出
149326


但是,从将xml复制到剪贴板开始,编辑->粘贴特殊->将xml粘贴为类,然后查看它生成了什么,然后从那里开始工作可能会更简单。

我无法复制您看到的确切错误,因此您可能需要从最小的复制中进行检查;我正在使用:

var env=(GLXmlResponse.Envelope)新的XmlSerializer(typeof(GLXmlResponse.Envelope))
.反序列化(新的StringReader(xml));
System.Console.WriteLine(env.Body.TransactionResponse.Response.Header.CoreJournal);
其中:

stringxml=@”
149326
";
对于
.Response
,它只有一个
null
。原因是这实际上是在空名称空间中(前缀不是继承的),因此需要

[xmlement(ElementName=“response”,Namespace=”“)]
公共响应{get;set;}
但是,这会导致xml中的
OKMessage
出现问题。如果我注释掉
,那么它会工作,我可以看到输出
149326


但是,从将xml复制到剪贴板开始,编辑->粘贴特殊->将xml粘贴为类,然后查看它生成了什么,然后从那里开始工作可能会更简单。

XmlRoot和xmlement有一个需要设置的名称空间属性。它可以是URL或空字符串“”。您可能需要添加空字符串以使代码正常工作。Xml序列化很难调试。所以我通常做的是从根类开始,注释掉类中的所有属性,让代码只与根一起工作。然后慢慢删除注释以找出错误发生的位置。XmlRoot和XmlElement具有需要设置的名称空间属性。它可以是URL或空字符串“”。您可能需要添加空字符串以使代码正常工作。Xml序列化很难调试。所以我通常做的是从根类开始,注释掉类中的所有属性,让代码只与根一起工作。然后慢慢删除注释以找出错误发生的位置。我仍然在使用“粘贴特殊xml生成的类”时遇到相同的错误。我仍然在使用“粘贴特殊xml生成的类”时遇到相同的错误