c# - 使用 XmlSerializer 将 DataTable 反序列化为自定义类

标签 c# datatable deserialization xmlserializer

是否可以将 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/

相关文章:

c# - 在 .NET 中反序列化 RSS 提要

c# - Rx - 暂停 Observable.Interval

c# - 如何只比较 sql server 2008 中日期时间字段的年份?

c# - 将数据表行插入数据库的简单方法

jquery - 初始化后更改数据表的设置

C# 在父构造函数中访问调用者

javascript - X-Editable 和 Bootstrap 数据表

java - OSGi 中的反序列化问题

c# - 反序列化 KeyValuePair<string, string> Json.Net

java - 强制 GSON 使用特定的构造函数