1
votes
<A xmlns="http://www.aaa.com/bbb/"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">    
<B>
  <aa>AUH</aa><bb>5</bb><cc>Abu</cc>
</B>
<B>
  <aa>AUH</aa><bb>7</bb><cc>ghi</cc>
</B>
  <ServiceResponse><ErrorMessage i:nil="true"/><ExecutionStatus>SUCCESS</ExecutionStatus></ServiceResponse>
</A>

I have a XML Like Above and I have created classes For A, B and ServiceResponse.

And I am trying to parse using the below code:

XmlSerializer ser = new XmlSerializer(typeof(A));
objAirportListResponse = ser.Deserialize(new StringReader(str)) as A;

In above code "str" is the string which contain above XML.

I am getting error like below:

There is an error in XML document (1, 2)(System.InvalidOperationException)

If i remove the xmlns="http://www.aaa.com/bbb/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" From the XML i got the same error at because of i:nil="true". Then i removed xmlns="http://www.aaa.com/bbb/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" and i:nil="true" i tried to parse the XML, This time it parsed in to class successfully.

But removing xmlns="http://www.aaa.com/bbb/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" and i:nil="true" is not a correct way, Please help me how parse above Example.

Thanks in Advance.

1
And what is the exception? InvalidOperationException.Message and InvalidOperationException.InnerException probably tell you exactly what's wrong.user1228
this is my exception:{System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: <A xmlns='aaa.com/bbb'> was not expected. at System.Xml.Serialization.XmlSerializer.resolveDeserializingType(XmlReader reader, XmlSerializationReader serialReader, Boolean soap12)Avinash
See if this question answers your problem: stackoverflow.com/questions/1556874/… Also, I've found the XmlSerializer is too finicky to be worth it. I find that parsing manually using Linq to XML is usually easier.user1228

1 Answers

0
votes

I agree with Will in the comments above about the Linq-to-XML approach being more reliable, but this link will probably work for you:

Use multiple namespaces when deserializing with .NET XmlSerializer

You'll have to attribute up your class with Xml attributes before deserializing it to get the data to read in. I suggest working the other way for a while - try serializing your class to XML over and over making tweaks along the way until you get exactly the output you're looking to read in, and then try to read the "real" data in.