2015-04-29 35 views
1

是否可以將DataTable反序列化爲自定義類?一個SOAP請求的格式返回的DataTable使用XmlSerializer將DataTable反序列化爲自定義類

<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> 

我想它反序列化爲使用XmlSerializer的

XmlSerializer serializer = new XmlSerializer(typeof(Geolocation[])) 
var Geolocations = (Geolocation)serializer.Deserialize(stream); 

編輯的的

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

陣列:我試圖做類似這個:

public class Geolocation { 
    [XmlElement("Latitude")] 
    public double Latitude { get; set; } 
    [XmlElement("Longitude")] 
    public double Longitude { get; set; } 
} 

但這不起作用

+2

好吧,那麼你到目前爲止嘗試過什麼? – aevitas

+0

@aevitas編輯 – Almis

回答

1

隨着aevitas的幫助和一些更多的研究,我終於成功地做到這一點,先寫你的服務引用像這樣

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; } 
} 

然後只寫

// Namespace url is optional 
XmlSerializer serializer = new XmlSerializer(typeof(DataTable), "http://example.com"); 
DataTable dt = (DataTable)serializer.Deserialize(stream); 
+0

嗨@almis,你如何創建反序列化的流?謝謝! – soydachi

+0

@dachibox流是來自httpwebrequest的響應流 – Almis

0

要使XmlSerializer正常工作,您需要一個正確裝飾並匹配XML的C#類。

例如,微軟提供了以下樣品:

public class PurchaseOrder 
{ 
    public Address MyAddress; 
} 
public class Address 
{ 
    public string FirstName; 
} 

對於下面的XML:

<PurchaseOrder> 
    <Address> 
     <FirstName>George</FirstName> 
    </Address> 
</PurchaseOrder> 
你的情況

所以,你聲明包含Geolocation對象NewDataSet類(或一個集合,如果你期望收到多個)包含一個LatitudeLongitude雙。

請確保您正確裝飾相關類型(如您在示例中所示),然後按您在問題中演示的反序列化它。

它應該這樣工作。

+0

但它開始於'diffgr:diffgram'我怎麼能聲明這樣的類? – Almis

+0

@Almis看到[這個問題](http://stackoverflow.com/questions/2339782/xml-serialization-and-namespace-prefixes)的更多細節。 – aevitas

0

你需要用一個數組或列表<父>對象

public class Root 
 
    { 
 
     [XmlElement("Geolocation")] 
 
     List<Geolocation> Geolocation { get; set; } 
 
    } 
 
    public class Geolocation 
 
    { 
 
     [XmlElement("Latitude")] 
 
     public double Latitude { get; set; } 
 
     [XmlElement("Longitude")] 
 
     public double Longitude { get; set; } 
 
    }

相關問題