Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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">
 <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.

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?

like image 895
Gareth Avatar asked May 12 '09 10:05

Gareth


1 Answers

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...

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

... which addresses the file parsing.

like image 188
Martin Peck Avatar answered Oct 18 '22 17:10

Martin Peck