查询反序列化 objects

Querying deserialized objects

有这个XML含车collection:

<?xml version="1.0" encoding="utf-8"?>
<CarCollection>
    <Cars>
        <Car>
            <StockNumber>1020</StockNumber>
            <Make>Nissan</Make>
            <Model>Sentra</Model>
        </Car>
        <Car>
            <StockNumber>1010</StockNumber>
            <Make>Toyota</Make>
            <Model>Corolla</Model>
        </Car>
        <Car>
            <StockNumber>1111</StockNumber>
            <Make>Honda</Make>
            <Model>Accord</Model>
        </Car>
        <Car>
            <StockNumber>2000</StockNumber>
            <Make>Maybach</Make>
            <Model>S 600</Model>
        </Car>
        <Car>
            <StockNumber>2001</StockNumber>
            <Make>Ferrari</Make>
            <Model>F355 Spider</Model>
        </Car>
    </Cars>
</CarCollection>

及其反序列化的 object classes,由 xsd.exe:

生成
//------------------------------------------------------------------------------
// <auto-generated>
//     Dieser Code wurde von einem Tool generiert.
//     Laufzeitversion:4.0.30319.42000
//
//     Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
//     der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// Dieser Quellcode wurde automatisch generiert von xsd, Version=4.6.1055.0.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class CarCollection : object, System.ComponentModel.INotifyPropertyChanged {

    private CarCollectionCars[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Cars", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public CarCollectionCars[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
            this.RaisePropertyChanged("Items");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class CarCollectionCars : object, System.ComponentModel.INotifyPropertyChanged {

    private CarCollectionCarsCar[] carField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Car", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public CarCollectionCarsCar[] Car {
        get {
            return this.carField;
        }
        set {
            this.carField = value;
            this.RaisePropertyChanged("Car");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class CarCollectionCarsCar : object, System.ComponentModel.INotifyPropertyChanged {

    private string stockNumberField;

    private string makeField;

    private string modelField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string StockNumber {
        get {
            return this.stockNumberField;
        }
        set {
            this.stockNumberField = value;
            this.RaisePropertyChanged("StockNumber");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Make {
        get {
            return this.makeField;
        }
        set {
            this.makeField = value;
            this.RaisePropertyChanged("Make");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Model {
        get {
            return this.modelField;
        }
        set {
            this.modelField = value;
            this.RaisePropertyChanged("Model");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

我想查询汽车的库存编号、品牌和型号class。所以, 我从这段代码开始:

XmlSerializer ser = new XmlSerializer(typeof(CarCollection));
CarCollection cars;
using (XmlReader reader = XmlReader.Create(file))
{
    cars = (CarCollection)ser.Deserialize(reader);
    foreach (var item in cars.Items)
    {
        foreach (var carCollection in item.Car)
        {
            carPool.Add(new CarPool()
            {
                Make = carCollection.Make,
                Model = carCollection.Model,
                StockNumber = carCollection.StockNumber
            });
        }
    }
}

但是,我不喜欢这种嵌套的 for 循环。有谁知道如何使用 LINQ 更有效地查询?

如果你想解析 XML 并且如果你使用 XmlSerializer 那么你需要管理 class 层次结构及其繁琐的工作来管理复杂和大的 class 层次结构xml

所以一种选择是使用 XDocument

您可以简单地在 XDocument 上使用 LINQ 从 XML 中获取所需的数据。

通过使用 XDocument,您可以直接专注于您的数据,而无需管理 class 结构。

XDocument xdoc = XDocument.Load(@"Path to your xml file");

var result = (from car in xdoc.Descendants("Car")
              select new
              {
                  StockNumber = car.Element("StockNumber")?.Value,
                  Make = car.Element("Make")?.Value,
                  Model = car.Element("Model")?.Value
              }).ToList();


Console.WriteLine("StockNumber\tMake\t\tModel");
Console.WriteLine("----------------------------------------------");
foreach (var item in result)
{
    Console.WriteLine(item.StockNumber + "\t\t" + item.Make + "\t\t" + item.Model); ;
}

选择:

你仍然想用 XmlSerializer 和 class 反序列化你的 xml 然后下面是 class 结构,

[XmlRoot("Car")]
public class Car
{
    [XmlElement("StockNumber")]
    public string StockNumber { get; set; }
    [XmlElement("Make")]
    public string Make { get; set; }
    [XmlElement("Model")]
    public string Model { get; set; }
}
[XmlRoot("Cars")]
public class Cars
{
    [XmlElement("Car")]
    public List<Car> Car { get; set; }
}
[XmlRoot("CarCollection")]
public class CarCollection
{
    [XmlElement("Cars")]
    public Cars Cars { get; set; }
}

用法:

XmlSerializer ser = new XmlSerializer(typeof(CarCollection));

CarCollection cars = null;

using (StreamReader reader = new StreamReader(@"Path to your xml file"))
{
    cars = (CarCollection)ser.Deserialize(reader);
}

List<Car> result = cars.Cars.Car.ToList();

//--------------------------Print the result to console--------------

Console.WriteLine("StockNumber\tMake\t\tModel");
Console.WriteLine("----------------------------------------------");
foreach (var item in result)
{
    Console.WriteLine(item.StockNumber + "\t\t" + item.Make + "\t\t" + item.Model); ;
}

Console.ReadLine();

输出:

Linq 和 SelectMany() 应该解决嵌套循环:

// not tested or compiled
using (XmlReader reader = XmlReader.Create(file))
{
    var cars = (CarCollection)ser.Deserialize(reader);

    carPool.AddRange(
       cars.Items.SelectMany(it => it.Car).Select(c => 
            new CarPool()
            {
                Make = c.Make,
                Model = c.Model,
                StockNumber = c.StockNumber
            }) );
}