C#:反序列化XML文件错误(认为这是一个命名空间问题 - 但是我的生活不能解决它)

Gar*_*eth 6 c# xml web-services xml-serialization

我正在对一个XML文件进行反序列化,该文件来自我们的一个客户的Web服务.

问题是,在使用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>
Run Code Online (Sandbox Code Playgroud)

顶级:

[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 {
Run Code Online (Sandbox Code Playgroud)

我认为有一个错误:

[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)]
Run Code Online (Sandbox Code Playgroud)

XML的一部分,但我不知道要改变什么 - 或者VS想要什么.

任何帮助,所有赞赏的人,我仍然是这一切的新手,我的老板一直在呼吸我,让这个工作:(

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

{"<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'> was not expected."}
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*eck 6

soap信封不是序列化对象的一部分.它是SOAP传输协议的一部分.您需要从信封中删除对象,而不是让对象处理信封.

您不需要获取整个xml文件(由于某种原因,包括soap信封),而是需要将第一个子项放在soap:body中并使用THAT反序列化到您的对象中.

查看此SO帖子......

使用C#和XDocument/XElement来解析Soap响应

...解决文件解析的问题.

  • 哦,我现在感觉有点傻:)除了手动读取文件,然后提取我想要的节点并将它们传递给反序列化器之外 - 是否还有另一种不太脏的方法来做到这一点? (2认同)