c# - Is that possible to have two namespaces in the class XMLTypeAttribute to handle deserialization of 2 soap responses? -


i have class has following declaration:

[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 bodyfield;      /// <remarks/>     public envelopebody body     {                 {             return this.bodyfield;         }         set         {             this.bodyfield = value;         }     } }  . . code generated here based on response xml... .  [system.xml.serialization.xmltypeattribute(anonymoustype = true, namespace = "order")] [system.xml.serialization.xmlrootattribute(namespace = "order", isnullable = false)] public partial class insertresponse {      private insertresponseout outfield;      /// <remarks/>     public insertresponseout @out     {                 {             return this.outfield;         }         set         {             this.outfield = value;         }     } } 

when deserializing xml response containing <insertresponse xmlns="order">, can that.

i have xml soap response, of same format , want use same class, different xmltypeattribute:

[system.xml.serialization.xmltypeattribute(anonymoustype = true, namespace = "customer")] [system.xml.serialization.xmlrootattribute(namespace = "customer", isnullable = false)] public partial class insertresponse {      private insertresponseout outfield;      /// <remarks/>     public insertresponseout @out     {                 {             return this.outfield;         }         set         {             this.outfield = value;         }     } } 

currently have method handling deserialization of soap response:

private envelope deserializesoapresponse<t>(string soapresponse) {      var serealizer = new xmlserializer(typeof(t));      envelope result;       using (textreader reader = new stringreader(soapresponse))      {          result = (envelope)serealizer.deserialize(reader);      }       return result;  } 

soapresponse parameter not path xml, string representing xml soap response server.

i also, tried use custom xml reader:

public class customxmlreader: system.xml.xmltextreader {     public customxmlreader(string url) : base(url) { }      public override string namespaceuri     {                 {             if (base.namespaceuri == "order")                 return "customer";              return base.namespaceuri;         }     } } 

as suggested, have following error: illegal character in path, since, expect, need send url soap response, sending string

how can that? possible define multiple namespaces xmltypeattribute or xmlrootattribute

for purpose can use custom xml reader.

public class customxmlreader : xmltextreader {     // define other required constructors     public customxmlreader(string url) : base(url) { }     public customxmlreader(textreader reader) : base(reader) { }      public override string namespaceuri     {                 {             if (base.namespaceuri == "order")                 return "customer";              return base.namespaceuri;         }     } } 

it converts order namespace customer namespace on fly.

accordingly, class should have 1 customer namespase declaration:

[xmltype(anonymoustype = true, namespace = "customer")] [xmlroot(namespace = "customer", isnullable = false)] public partial class insertresponse 

use usual:

string xml = "your xml here";  insertresponse response;  using (var stringreader = new stringreader(xml)) using (var xmlreader = new customxmlreader(stringreader))     response = (insertresponse)xs.deserialize(xmlreader); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -