如何将数据集强制转换为强类型对象,例如List< Location> ? [英] How to cast dataset to strongly typed object, say, List<Location> ?

查看:142
本文介绍了如何将数据集强制转换为强类型对象,例如List< Location> ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的名为PropertyInfo.xml的xml文件: -

Following is my xml file called PropertyInfo.xml :-

<?xml version="1.0" encoding="utf-8" ?>
<PropertyInformation>
<locations>
  <location name="Bombay">
    <Buildings>
      <Building name="Majestic House">
        <Rooms>
          <Room name="Delmonte Conference Room">
            <Capacity>325</Capacity>
          </Room>
          <Room name="Majestic Ballroom">
            <Capacity>150</Capacity>
          </Room>
        </Rooms>
      </Building>
      <Building name="Taj Mahal Palace Hotel">
        <Rooms>
          <Room name="THE LUXURY GRANDE">
            <Capacity>4</Capacity>
          </Room>
        </Rooms>
      </Building>
    </Buildings>
  </location>
  <location name="New York">
    <Buildings>
      <Building name="City Group Center">
        <Rooms>
          <Room name="LeMessurier Hall">
            <Capacity>210</Capacity>
          </Room>
        </Rooms>
      </Building>
    </Buildings>
  </location>
</locations>
</PropertyInformation>



我创建了以下4个模型来映射上面的xml文件: -


I created following 4 Models to map above xml file :-

public class Room
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  public int Capacity { get; set; }
}

public class Building
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  public List<Room> Rooms { get; set; }
}

public class Location
{
  [XmlAttribute("name")]
  public string Name { get; set; }
  public Building Buildings { get; set; }
}

[XmlRoot("PropertyInformation")]
public class PropertyInformation
{
  [XmlArray("locations")]
  [XmlArrayItem("location")]
  public List<Location> Locations { get; set; }
}



我在DAL类之后创建以从PropertyInfo.xml加载信息: -


I created following DAL class to load the information from PropertyInfo.xml :-

public class PropertyInfoDal
{
  public List<Location> LoadPropertyInfo()
  {
    DataSet ds = new DataSet();
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet));
    FileStream readStream = new FileStream("~/App_Data/PropertyInfo.xml", FileMode.Open);
    ds = (DataSet) xmlSerializer.Deserialize(readStream);
    readStream.Close();

    //How to Cast DataSet to Strongly Typed Object( List<Location>) before it gets returned ??
    //I started with this much below
    List<Location> locations = (from row in ds.Tables[0].AsEnumerable()
    select new PropertyLocation
    {
      Name = row.Field<String>("Name"),
      Buildings = ?????
    }).ToList();

    return locations;
  }
}



在上面的LoadPropertyInfo()方法中,如何将DataSet转换为List< Location>返回位置之前的位置?请帮忙。谢谢!


In above LoadPropertyInfo() method, how to cast DataSet to List<Location> locations before returning "locations" ? Pls help. Thanks!

推荐答案

为什么要打扰数据集呢。您可以直接反序列化到您的对象,如下所示:

Why to bother with the dataset at all. You can deserialize directly to your objects, like this:
var xmlSerializer = new XmlSerializer(typeof(PropertyInformation));
var propInfo = (PropertyInformation) xmlSerializer.Deserialize(readStream);
return propInfo.Locations; 



您需要为您的班级添加几个属性:


You need to add couple of attributes to your classes however:

public class Room
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  public int Capacity { get; set; }
}
 
public class Building
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  [XmlArray("Rooms")]
  [XmlArrayItem("Room")]
  public List<Room> Rooms { get; set; }
}
 
public class Location
{
  [XmlAttribute("name")]
  public string Name { get; set; }
  [XmlArray("Buildings")]
  [XmlArrayItem("Building")]
  public List<Building> Buildings { get; set; }
}
 
[XmlRoot("PropertyInformation")]
public class PropertyInformation
{
  [XmlArray("locations")]
  [XmlArrayItem("location")]
  public List<Location> Locations { get; set; }
}


您对实体模型的定义是错误的。我建议使用 xsd.exe [ ^ ],这有助于创建与xml架构相关的类。



如需了解更多信息,请参阅:

使用xsd.exe自动生成实体类用于XML序列化和反序列化 [ ^ ]

XML序列化和反序列化:第1部分 [ ^ ]

XML序列化和反序列化:第2部分 [ ^ ]



这是自定义类集合序列化和反序列化的完整示例 [ ^ ],虽然在VB.NET中,但是这种语言类似于C#。
Your definition of entity model is wrong. I'd suggest to use xsd.exe[^], which helps to create class(es) related to xml schema.

For further information, please see:
Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization[^]
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]

Here is A Complete Sample of Custom Class Collection Serialization and Deserialization[^], although in VB.NET, but this language is similar to C#.


这篇关于如何将数据集强制转换为强类型对象,例如List&lt; Location&gt; ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆