0

this is the string:

string incomingOrdreXML = @"<GetOrderStatus xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/OrderStatusConsole""><AccountID>5</AccountID><Batch></Batch><Date></Date><OrderNumber></OrderNumber><Password></Password><Sequence></Sequence><StatusCode></StatusCode><Test></Test><TrackingID></TrackingID></GetOrderStatus>";

this is the class:

public class GetOrderStatus
    {
        public string AccountID { get; set; }
        public string Password { get; set; }
        public string Batch { get; set; }
        public string Sequence { get; set; }
        public string Test { get; set; }
        public string Date { get; set; }
        public string OrderNumber { get; set; }
        public string StatusCode { get; set; }
        public string TrackingID { get; set; }
    }

this is the deserialization code:

string incomingOrdreXML = @"<GetOrderStatus xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/OrderStatusConsole""><AccountID>5</AccountID><Batch></Batch><Date></Date><OrderNumber></OrderNumber><Password></Password><Sequence></Sequence><StatusCode></StatusCode><Test></Test><TrackingID></TrackingID></GetOrderStatus>";

            var reader = new StringReader(incomingOrdreXML);
            XmlSerializer serializer = new XmlSerializer(typeof(GetOrderStatus));
            var instance = (GetOrderStatus)serializer.Deserialize(reader);

Fails in the last line, says

{"<GetOrderStatus xmlns='http://schemas.datacontract.org/2004/07/OrderStatusConsole'> was not expected."}

This line will change depending on who sends this XML. Meaning if the sender is OrderStatusConsole then it's like above if the sender is Jeff then it will have Jeff in the end.

Any clues?

Thanks in advance.

Codehelp
  • 4,157
  • 9
  • 59
  • 96

4 Answers4

0

It can't deserialize if you don't have the correct namespace defined, either remove the xmlns='..' line, or use this:

[XmlRootAttribute(Namespace = 
    "http://schemas.datacontract.org/2004/07/OrderStatusConsole")]
public class GetOrderStatus
    {

       ...
    }
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
0

My guess is you need to ignore those namespaces. I believe a solution has been mentioned here:

Can I make XmlSerializer ignore the namespace on deserialization?

Community
  • 1
  • 1
Wolf5
  • 16,600
  • 12
  • 59
  • 58
0

You can try instantiating the XmlSerializer object like this:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "GetOrderStatus";
xRoot.Namespace = "http://schemas.datacontract.org/2004/07/OrderStatusConsole";

XmlSerializer serializer = new XmlSerializer(typeof(GetOrderStatus), xRoot);

Now, if you know who the sender is, you can change the namespace prior to instantiating the serializer.

Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98
0

You could have a look at the xsd tool

xsd myFile.xml /outputdir:myOutputDir
xsd myFile.xsd /outputdir:myOutputDir /classes

this will generate an xsd or a set of classes that the a given piece of xml can always be deserialized into. If you run on several sets of your incoming xml and compare the generated xsds you should be able to come up with something that will work for all of them by adding any missing elements and sprinkling in some minOccurs="0" or nillable=”true.

Miniver Cheevy
  • 1,667
  • 2
  • 14
  • 20