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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:12:10  来源:igfitidea点击:

C#: Deserialise XML File error (think it's a namespace issue - cant for the life of me work it out though)

c#xmlweb-servicesxml-serialization

提问by Gareth

I'm deserialising an XML file which comes from a webservice of one of our clients.

我正在反序列化来自我们一位客户的网络服务的 XML 文件。

Problem is, after creating the class with xsd.exe I deserialise the file and get the usual "There is an error in XML document (2, 2)." visual studio error. This, I presume is line 2, which points to the namespace declarations:

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

Top of XML file:

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>

Top Of Class:

一流的:

[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 {

I presume there's an error with the:

我认为以下内容存在错误:

[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)]

part of the XML, but I have no idea what to change it to - or what VS wants.

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

any help at all appreciated guys, I'm still pretty new to all this and my boss is breathing down me all the time to get this working :(

任何对所有感激的人的帮助,我对这一切还是很陌生,我的老板一直在让我喘不过气来让我工作:(

EDIT:There is an inner exception yes! Sorry guys!

编辑:有一个内部异常是的!对不起大家!

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

So how do I add this namespace declaration to the class?

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

采纳答案by Martin Peck

The soap envelope is not part of your serialized object. It's part of the SOAP transport protocol. You need to remove your object from the envelope, rather than making your object deal with the envelope.

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

Instead of taking the entire xml file (which, for some reason, includes the soap envelope) you need to take the first child within the soap:body and use THAT to deserialize into your object.

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

Check out this SO post...

看看这个SO帖子......

Using C# and XDocument/XElement to parse a Soap Response

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

... which addresses the file parsing.

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