Linq To Xml vs Linq To Sql

时间:2009-11-05 21:45:10

标签: xml linq serialization

尝试“解冻”已存储在XML文件中的序列化对象:

在LinqToSQL中,我可以(使用适当装饰的类)执行此类操作:

   [Table(Name="Plants")]
   public class Plant
   {
      [Column(Name = "Id", IsPrimaryKey = true)]
      public int Id { get; set; }
      [Column(Name = "Genus")]
      public string Genus { get; set; }
      [Column(Name = "Species")]
      public string Species { get; set; }
      [Column(Name = "CommonName")]
      public string CommonName { get; set; }
      ...
    }

然后再这样做:

 using (DataContext db = new DataContext(ConnectString))
 {
    plants = (
       from d in db.GetTable<Plant>()
       select d).ToList();
 }

并且来自SQL表的数据用于填充List&lt;&gt;植物对象。

但是使用LinqToXML我似乎无法做到这一点,而是我必须做这样的事情:

<!-- XML File -->
<Plants>
   <Plant>
      <Genus>Quercus</Genus>
      <Species>alba</Species>
      <CommonName>White Oak</CommonName>
   </Plant>
   ...
</Plants>

   // Class
   [DataContract]
   [XmlRoot("Plants")]
   public class Plant
   {
      [DataMember]
      public string Genus { get; set; }
      [DataMember]
      public string Species { get; set; }
      [DataMember]
      public string CommonName { get; set; }
      ...
    }

 // Code -- note: I already have the XML file in an XDocument called "doc"
 IEnumerable<XElement> items = (from item in doc.Descendants("Plant")
   where item.Element("Genus").Value.Equals("Quercus")
   select item);

 List<Plant> plants = new List<Plant>();
 foreach (XElement item in items)
 {
    Plant a = new Plant();
    a.Genus = item.Element("Genus").Value;
    a.Species = item.Element("Species").Value;
    XElement ex = item.Element("CommonName");
    if ((null == ex) || ex.IsEmpty) { } else { a.Example = ex.Value; }
    plants.Add(a);
 }

鉴于我希望将此序列化/反序列化作为通用目的,有没有办法在不借助反射的情况下执行此操作?我可以使用XmlSerializer,但是,这也涉及到很多乱码MemoryStreams并将它们用作XmlWriter或XmlReaders,当我想要的是让我的类自动转到/来自XML文件。只是想知道我是否在这里没有连接点......

1 个答案:

答案 0 :(得分:1)

我担心你混淆了DataContracts和LinqToXML。

您可以使用DataContracts将Xml文档反序列化为对象。

反序列化后,您应该可以使用LinqToObjects来查询数据集。

尝试一下:

[CollectionDataContract(Name = "Plants", ItemName = "Plant", Namespace = "")]
class Plants: List<Plant> { }

[DataContract(Name = "Plant", Namespace = "")]
class Plant
{
    [DataMember()]
    public string Genus { get; set; }

    [DataMember()]
    public string Species { get; set; }

    [DataMember()]
    public string CommonName { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (var fs = new FileStream("Plants.xml", FileMode.Open))
        using (var reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()))
        {
            var ser = new DataContractSerializer(typeof(Plants));

            var plants = ser.ReadObject(reader) as Plants;

            foreach (var plant in plants)
                Console.WriteLine("{0}, {1}, {2}", plant.Genus, plant.Species, plant.CommonName);
        }

        Console.ReadKey();
    }
}