Reputation: 6745
I'm trying to Deserialize a SoapException message that I am receiving from a web service. I'm getting the following exception:
System.InvalidOperationException was unhandled
Message="There is an error in XML document (2, 2)."
Source="System.Xml"
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at FullServiceServices.AddressCorrectionService.GetResponse(FullServiceAddressCorrectionQueryRequest request) in AddressCorrectionService.cs:line 169
at FullServiceServices.AddressCorrectionService.GenerateRequest(DateTime startDate, DateTime endDate) in AddressCorrectionService.cs:line 79
at FullServiceServices.Form1.Form1_Load(Object sender, EventArgs e) in \Form1.cs:line 33
InnerException: System.InvalidOperationException
Message="<Fault xmlns='http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml'> was not expected."
Source="gn4ggxxc"
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFault.Read3_Fault()
How can I properly deserialize this XML into the provided partial class Fault?
Below is how I'm reading the XML string in to the XMLSerializer:
XmlTextReader xr = new XmlTextReader(new StringReader(soapEx.Detail.FirstChild.InnerText));
XmlSerializer xs = new XmlSerializer(typeof(Fault));
Fault fault = xs.Deserialize(xr) as Fault;
Here is the InnerText of the soap exception .Detail property's FirstChild
<?xml version="1.0" encoding="UTF-8"?>
<mailxml:Fault xmlns:mailxml="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml" xmlns:mailxml_base="http://idealliance.org/maildat/Specs/md091/mailxml81/base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml mailxml_031910.xsd http://idealliance.org/maildat/Specs/md091/mailxml81/base mailxml_base_031910.xsd">
<mailxml:FaultCode>402</mailxml:FaultCode>
<mailxml:FaultDescription>402 Not Well Formed XML</mailxml:FaultDescription>
</mailxml:Fault>
Here is the partial class that is auto-generated via WSDL.
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml")]
public partial class Fault {
private string[] faultCodeField;
private string[] faultDescriptionField;
private string trackingIDField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FaultCode")]
public string[] FaultCode {
get {
return this.faultCodeField;
}
set {
this.faultCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FaultDescription")]
public string[] FaultDescription {
get {
return this.faultDescriptionField;
}
set {
this.faultDescriptionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string TrackingID {
get {
return this.trackingIDField;
}
set {
this.trackingIDField = value;
}
}
}
Thanks!
Upvotes: 3
Views: 4903
Reputation: 6745
Setting the namespace on the RootElement did the trick
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.Namespace = "http://namespacehere";
XmlReader xr = soapEx.Detail.FirstChild.CreateNavigator().ReadSubtree();
XmlSerializer xs = new XmlSerializer(typeof(Fault), xRoot);
Fault fault = xs.Deserialize(xr) as Fault;
Upvotes: 4
Reputation: 161773
First, don't use new XmlTextReader()
unless you have no other choices. It's been deprecated since .NET 2.0.
Second, you already have XML - why turn it into a string and then back into XML? Try
XmlReader xr = soapex.Detail.FirstChild.CreateNavigator().ReadSubTree();
Fault fault = xs.Deserialize(xr) as Fault;
Upvotes: 1