Strongly inspired How to deserialize an XML document , I think this should do the trick.
I generated the class as follows:
- Save the Xml server response to disk (c: \ temp \ reply.xml)
- xsd c: \ temp \ reply.xml / o: "c: \ temp"
- xsd c: \ temp \ reply.xsd reply_app1.xsd / classes / o: "c: \ temp"
- Add reply_app1.cs to your project.
The result is:
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.269 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by xsd, Version=4.0.30319.1. // /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] [System.Xml.Serialization.XmlRootAttribute(Namespace="http://schemas.xmlsoap.org/soap/envelope/", IsNullable=false)] public partial class Envelope { private EnvelopeBody[] itemsField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("Body")] public EnvelopeBody[] Items { get { return this.itemsField; } set { this.itemsField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] public partial class EnvelopeBody { private EnvelopeBodyFault[] faultField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("Fault")] public EnvelopeBodyFault[] Fault { get { return this.faultField; } set { this.faultField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] public partial class EnvelopeBodyFault { private string faultcodeField; private string faultstringField; private EnvelopeBodyFaultDetail detailField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string faultcode { get { return this.faultcodeField; } set { this.faultcodeField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string faultstring { get { return this.faultstringField; } set { this.faultstringField = value; } } /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public EnvelopeBodyFaultDetail detail { get { return this.detailField; } set { this.detailField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] public partial class EnvelopeBodyFaultDetail { private InnerException innerExceptionField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Namespace="http://my.namespace.com")] public InnerException InnerException { get { return this.innerExceptionField; } set { this.innerExceptionField = value; } } } /// <remarks/> [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://my.namespace.com")] [System.Xml.Serialization.XmlRootAttribute(Namespace="http://my.namespace.com", IsNullable=false)] public partial class InnerException { private string messageField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string message { get { return this.messageField; } set { this.messageField = value; } } }
Yes, it is generated automatically, but I decided that it would be the easiest way to create a class that you could deserialize. What you can do as follows:
XmlDocument document = new XmlDocument(); document.Load(Server.MapPath("~/Data/reply.xml")); System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Envelope)); Envelope envelope = (Envelope)serializer.Deserialize(new StringReader(document.OuterXml));
What took the Detail property correctly. In your case, you need to use the XmlReader , as you did before, instead of my new StringReader(document.OuterXml) , and wrap it in a use block, etc.
Keep in mind that some property names of the deserialized object will not exactly match what you had in your SoapFault class (now it is called Envelope , for example), and some of the properties are expressed as arrays, not separate objects, but you, probably could configure Xsd if necessary.