C#:反序列化 XML 文件错误(认为这是命名空间问题 - 但我一生都无法解决)

2023-12-20

我正在反序列化一个来自我们客户之一的 Web 服务的 XML 文件。

问题是,在使用 xsd.exe 创建类后,我反序列化该文件并得到通常的“XML 文档中存在错误 (2, 2)”。视觉工作室错误。我认为这是第 2 行,它指向命名空间声明:

XML 文件顶部:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
 <soapenv:Body><MXWorkorderOutResp language="EN" xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header event="0" operation="Response" rsCount="8" rsStart="0" rsTotal="8">
    <SenderID build="127" dbbuild="V600-467" majorversion="6" minorversion="1" type="MAXIMO">MX</SenderID>
    <CreationDateTime>2009-05-11T09:48:51+01:00</CreationDateTime>
    <RecipientID>SUPPLIER</RecipientID>
    <MessageID>12420317323327108</MessageID>
  </Header>
  <Content>
    <MXWORKORDER>
      <WORKORDER>

一流:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.mro.com/mx/integration")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.mro.com/mx/integration", IsNullable=false)]


public partial class MXWorkorderOutResp {

private MXWorkorderOutRespHeader[] headerField;

private MXWorkorderOutRespContentMXWORKORDERWORKORDER[][][] contentField;

private string languageField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Header")]
public MXWorkorderOutRespHeader[] Header {
    get {
        return this.headerField;
    }
    set {
        this.headerField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("MXWORKORDER", typeof(MXWorkorderOutRespContentMXWORKORDERWORKORDER[]), IsNullable=false)]
[System.Xml.Serialization.XmlArrayItemAttribute("WORKORDER", typeof(MXWorkorderOutRespContentMXWORKORDERWORKORDER), IsNullable=false, NestingLevel=1)]
public MXWorkorderOutRespContentMXWORKORDERWORKORDER[][][] Content {

我认为有一个错误:

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.mro.com/mx/integration")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.mro.com/mx/integration", IsNullable=false)]

XML 的一部分,但我不知道将其更改为什么 - 或者 VS 想要什么。

感谢大家的帮助,我对这一切还很陌生,我的老板一直在催促我让这一切顺利进行:(

EDIT:有一个内在的例外是的!对不起大家!

{"<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'> was not expected."}

那么如何将这个名称空间声明添加到类中呢?


肥皂信封不是序列化对象的一部分。它是 SOAP 传输协议的一部分。您需要将对象从信封中取出,而不是让对象与信封接触。

您不需要获取整个 xml 文件(由于某种原因,其中包括soap 信封),您需要获取soap:body 中的第一个子文件并使用它反序列化到您的对象中。

看看这个帖子...

使用 C# 和 XDocument/XElement 解析 Soap 响应 https://stackoverflow.com/questions/779976/using-c-and-xdocument-xelement-to-parse-a-soap-response

...它解决了文件解析问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C#:反序列化 XML 文件错误(认为这是命名空间问题 - 但我一生都无法解决) 的相关文章

随机推荐