Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why are you manually deserializing XML, when you have WSDL ?</p> <p>If you have WSDL, use the svcutil.exe tool, or the wsdl.exe tool, to generate proxy classes and DTOs for the XML messages being sent and received on the wire. </p> <p>The point of a web services toolkit, or "stack" is to provide this for you, so that you don't have to author classes and XML serialization code by hand. </p> <p>Did you try this? Did you try to run the WSDL through one of those tools? Or did you try to "Add web reference" in Visual Studio? </p> <hr> <p>After updating the question, I suggest that you modify the WSDL, rather than write custom code. You can produce a custom WSDL for the service, which will correctly generate the proxy classes you want. If you don't need all 100 methods (or however many there are), then leave them out. If you want a custom object from a method, then define a complexType that corresponds to that object. This is much simpler and more reliable than hand-authoring XML deserialization code for each method. </p> <hr> <p>If you don't like that idea, and want to stick with manually writin the XML deserialization code, then you need to do two things:</p> <ol> <li><p>attach <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute.namespace.aspx" rel="nofollow noreferrer">a namespace to the XmlRoot attribute</a>. </p></li> <li><p>change the name of your class to <code>ResponseExt</code>, and derive it from a class called <code>Response</code>. Decorate that Response class with an <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlincludeattribute.aspx" rel="nofollow noreferrer">XmlInclude</a> attribute. This aligns the use of <a href="http://msdn.microsoft.com/en-us/library/ca1ks327(VS.80).aspx" rel="nofollow noreferrer">the Xml Serializer with the xsi:type used in the XML fragment</a>. </p></li> </ol> <p>It looks like this in code: </p> <pre><code>[XmlRoot("Response", Namespace="http://www.thirdparty.com/lr/")] public class ResponseExt : Response { } [XmlRoot("Response", Namespace="http://www.thirdparty.com/lr/")] [XmlInclude(typeof(ResponseExt))] public class Response { public string Code {get; set;} public string Message {get; set;} public string SessionId {get; set;} } public class XsiType { public static void Main(string[] args) { try { string filename = "XsiType.xml"; XmlSerializer s1 = new XmlSerializer(typeof(Response)); ResponseExt r = null; using(System.IO.StreamReader reader= System.IO.File.OpenText(filename)) { r= (ResponseExt) s1.Deserialize(reader); } var builder = new System.Text.StringBuilder(); var xmlws = new System.Xml.XmlWriterSettings { OmitXmlDeclaration = true, Indent= true }; using ( var writer = System.Xml.XmlWriter.Create(builder, xmlws)) { //s1.Serialize(writer, r, ns); s1.Serialize(writer, r); } string xml = builder.ToString(); System.Console.WriteLine(xml); } catch (System.Exception exc1) { Console.WriteLine("Exception: {0}", exc1.ToString()); } } } </code></pre> <p>related: <a href="https://stackoverflow.com/questions/1943597">How can I force the use of an xsi:type attribute?</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload