c# - Deserialize DataTable to custom class using XmlSerializer -


is possible deserialize datatable custom class? 1 of soap requests return datatable in format

<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="newdataset">   <xs:element name="newdataset" msdata:isdataset="true" msdata:maindatatable="geolocation" msdata:usecurrentlocale="true">     <xs:complextype>       <xs:choice minoccurs="0" maxoccurs="unbounded">         <xs:element name="geolocation">           <xs:complextype>             <xs:sequence>               <xs:element name="latitude" type="xs:float" minoccurs="0"/>               <xs:element name="longitude" type="xs:float" minoccurs="0"/>             </xs:sequence>           </xs:complextype>         </xs:element>       </xs:choice>     </xs:complextype>   </xs:element> </xs:schema> <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">   <newdataset xmlns="">      <geolocation diffgr:id="geolocation" msdata:roworder="0">        <latitude>48.8186</latitude>        <longitude>28.4681</longitude>      </geolocation>      ...   </newdataset> </diffgr:diffgram> 

i deserialize array of

public class geolocation {     public double latitude { get; set; }     public double longitude { get; set; } } 

using xmlserializer

xmlserializer serializer = new xmlserializer(typeof(geolocation[])) var geolocations = (geolocation)serializer.deserialize(stream); 

edit: tried this:

public class geolocation {     [xmlelement("latitude")]     public double latitude { get; set; }     [xmlelement("longitude")]     public double longitude { get; set; } } 

but didnt work

with of aevitas , more research managed it, first write service references this

public class datatable {     [xmlelement(elementname = "schema", namespace = "http://www.w3.org/2001/xmlschema")]     public schema schema { get; set; }     [xmlelement(elementname = "diffgram", namespace="urn:schemas-microsoft-com:xml-diffgram-v1")]     public diffgram diffgram { get; set; } }  public class schema {     [xmlelement(elementname = "element", namespace = "http://www.w3.org/2001/xmlschema")]     public element element { get; set; } }  public class element {     [xmlelement(elementname = "complextype", namespace = "http://www.w3.org/2001/xmlschema")]     public complextype complextype { get; set; }     [xmlattribute(attributename = "name", namespace = "http://www.w3.org/2001/xmlschema")]     public string name { get; set; } }  public class complextype {     [xmlelement(elementname = "choice", namespace = "http://www.w3.org/2001/xmlschema")]     public choice choice { get; set; }     [xmlelement(elementname = "sequence", namespace = "http://www.w3.org/2001/xmlschema")]     public sequence sequence { get; set; } }  public class sequence {     [xmlelement(elementname = "element", namespace = "http://www.w3.org/2001/xmlschema")]     public element[] elements { get; set; } }  public class choice {     [xmlelement(elementname = "element", namespace = "http://www.w3.org/2001/xmlschema")]     public element element { get; set; } }  public classdiffgram {     [xmlelement(elementname = "newdataset", namespace = "")]     public newdataset newdataset { get; set; } }  public class newdataset {     [xmlelement("geolocation")]     public geolocation[] geolocations { get; set; } }  public class geolocation {     [xmlelement("latitude")]     public double latitude { get; set; }     [xmlelement("longitude")]     public double longitude { get; set; } } 

and write

// namespace url optional xmlserializer serializer = new xmlserializer(typeof(datatable), "http://example.com"); datatable dt = (datatable)serializer.deserialize(stream); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -