使用 XmlSerializer 将 DataTable 反序列化为自定义 class
Deserialize DataTable to custom class using XmlSerializer
是否可以将 DataTable 反序列化为自定义 Class? soap 请求之一 return 这种格式的 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; }
}
但这没有用
要使 XmlSerializer
正常工作,您需要一个经过适当修饰并与 XML.
相匹配的 C# class
例如,Microsoft 提供了以下示例:
public class PurchaseOrder
{
public Address MyAddress;
}
public class Address
{
public string FirstName;
}
以下XML:
<PurchaseOrder>
<Address>
<FirstName>George</FirstName>
</Address>
</PurchaseOrder>
所以在你的情况下,你会声明一个 NewDataSet
class 包含一个 Geolocation
对象(或者它的集合,如果你希望收到多个)其中包含一个 Latitude
和 Longitude
加倍。
确保正确装饰相关类型(就像您在示例中所做的那样),并按照您在问题中演示的那样对其进行反序列化。
应该是这样的。
您需要一个带有数组或 List<> 对象的父对象
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; }
}
在 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);
是否可以将 DataTable 反序列化为自定义 Class? soap 请求之一 return 这种格式的 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; }
}
但这没有用
要使 XmlSerializer
正常工作,您需要一个经过适当修饰并与 XML.
例如,Microsoft 提供了以下示例:
public class PurchaseOrder
{
public Address MyAddress;
}
public class Address
{
public string FirstName;
}
以下XML:
<PurchaseOrder>
<Address>
<FirstName>George</FirstName>
</Address>
</PurchaseOrder>
所以在你的情况下,你会声明一个 NewDataSet
class 包含一个 Geolocation
对象(或者它的集合,如果你希望收到多个)其中包含一个 Latitude
和 Longitude
加倍。
确保正确装饰相关类型(就像您在示例中所做的那样),并按照您在问题中演示的那样对其进行反序列化。
应该是这样的。
您需要一个带有数组或 List<> 对象的父对象
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; }
}
在 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);