是否可以将 DataTable 反序列化为自定义类?一个 SOAP 请求以这种格式返回 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>
我想将其反序列化为数组
public class Geolocation {
public double latitude { get; set; }
public double longitude { get; set; }
}
使用 XmlSerializer
XmlSerializer serializer = new XmlSerializer(typeof(Geolocation[]))
var Geolocations = (Geolocation)serializer.Deserialize(stream);
编辑:我尝试做这样的事情:
public class Geolocation {
[XmlElement("Latitude")]
public double Latitude { get; set; }
[XmlElement("Longitude")]
public double Longitude { get; set; }
}
但这没有用
最佳答案
在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);
关于c# - 使用 XmlSerializer 将 DataTable 反序列化为自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29942562/