views:

1672

answers:

1

Hey all,

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

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:

Top of XML file:

<?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"&gt;
 <soapenv:Body><MXWorkorderOutResp language="EN" xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  <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.

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 :(

Thanks

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

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

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

+2  A: 

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.

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.

Check out this SO post...

http://stackoverflow.com/questions/779976/using-c-and-xdocument-xelement-to-parse-a-soap-response

... which addresses the file parsing.

Martin Peck
oohhh I feel a little bit silly now :)Apart from manually reading the file, then extracting the nodes I want and passing them to the deserializer - is there another, less dirty feeling way of doing this?
Gareth
Is this for testing, or do you (for some reason) get your SOAP messages in file format (rather than as part of a ASMX/WCF stack)?
Martin Peck
this is, for some reason, exactly how the web service returns the XML. it wont integrate into VS using the "add web reference" so we have to manually POST the query and then read back the HTTPResponse into XML. So since it's not being parsed the "traditional" way I presume the soap headers are getting left in.it is truly a terrible and hacked to pieces system :(
Gareth
oh and Martin, thanks for your efforts with this, very very much appreciated! ^_^
Gareth