简体   繁体   English

序列化对象列表

[英]Serialize list of objects

I need to serialize/deserialize some XML code and part of it looks like next example: 我需要对一些XML代码进行序列化/反序列化,部分内容类似于下一个示例:

<CoordGeom>
    <Curve rot="cw" chord="830.754618036885" crvType="arc" delta="72.796763873948" dirEnd="283.177582669379" dirStart="355.974346543327" external="169.661846548051" length="889.38025007632" midOrd="136.562611151675" radius="699.999999998612" tangent="516.053996536113">
        <Start>4897794.2800513292 6491234.9390137056</Start>
        <Center>4897096.0071489429 6491185.7968343571</Center>
        <End>4897255.5861026254 6491867.3645547926</End>
        <PI>4897758.0514541129 6491749.7197593488</PI>
    </Curve>
    <Spiral length="109.418078418008" radiusEnd="INF" radiusStart="699.999999999025" rot="cw" spiType="clothoid" theta="4.477995782709" totalY="2.849307921907" totalX="109.351261203955" tanLong="72.968738862921" tanShort="36.493923980983">
        <Start>4897255.5861026254 6491867.3645547936</Start>
        <PI>4897220.0531303799 6491875.6840722272</PI>
        <End>4897147.9238984985 6491886.7208634559</End>
    </Spiral>
    <Spiral length="153.185309785019" radiusEnd="499.99999999993" radiusStart="INF" rot="ccw" spiType="clothoid" theta="8.776871734087" totalY="7.808812331497" totalX="152.826239431476" tanLong="102.249348442205" tanShort="51.176160975293">
        <Start>4897147.9238985004 6491886.7208634559</Start>
        <PI>4897046.8509311257 6491902.186455016</PI>
        <End>4896998.0370401107 6491917.5553683294</End>
    </Spiral>
    <Curve rot="ccw" chord="936.510896488672" crvType="arc" delta="138.94725576785" dirEnd="66.423714388543" dirStart="287.476458620693" external="925.970149937768" length="1212.543549877849" midOrd="324.680762068264" radius="499.999999999181" tangent="1335.436583485725">
        <Start>4896998.0370401107 6491917.5553683294</Start>
        <Center>4897148.1939981515 6492394.4755796343</Center>
        <End>4896948.2091376046 6492852.7397562303</End>
        <PI>4895724.243644949 6492318.6055583945</PI>
    </Curve>
</CoordGeom>

I've generated automatically classes using xsd.exe. 我已经使用xsd.exe自动生成了类。 Part of generated code looks like this: 生成的代码的一部分如下所示:

public partial class CoordGeom
    {
        private List<object> _items;
        private List<Feature> _feature;
        private string _desc;
        private string _name;
        private stateType _state;
        private string _oID;

        public CoordGeom()
        {
            _feature = new List<Feature>();
            _items = new List<object>();
        }

       [XmlElementAttribute("Chain", typeof(Chain))]
       [XmlElementAttribute("Curve", typeof(Curve))]
       [XmlElementAttribute("IrregularLine", typeof(IrregularLine))]
       [XmlElementAttribute("Line", typeof(Line))]
       [XmlElementAttribute("Spiral", typeof(Spiral))]
        public List<object> Items
        {
            get { return this._items; }
            set { this._items = value; }
        }

        [XmlElement("Feature")]
        public List<Feature> Feature { get; set; }

        [XmlAttribute()]
        public string desc { get; set; }

        [XmlAttribute()]
        public string name { get; set; }

        [XmlAttribute()]
        public stateType state { get; set; }

        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string oID
        {
            get{ return this._oID; }
            set{ this._oID = value; }
        }
    }

And my code for deserialization look like this: 我的反序列化代码如下所示:

XmlSerializer mySerializer = new XmlSerializer(typeof(LandXML), new XmlRootAttribute(""));
TextReader myFileStream = new StreamReader("myFile.xml");
LandXML myObject = (LandXML)mySerializer.Deserialize(myFileStream);
var coordGeomItems = myObject.Alignments.Alignment[0].CoordGeom;

My problem is that, when I deserialize file, it is deserialized as list of items of type {LandXML.Curve}, {LandXML.Spiral} etc. and I don't know how to access their properties. 我的问题是,当我反序列化文件时,它会反序列化为{LandXML.Curve},{LandXML.Spiral}等类型的项目列表,而我不知道如何访问它们的属性。 It would be great if I can do this directly. 如果我可以直接执行此操作,那就太好了。 Here is a screenshot: 这是屏幕截图:

调试屏幕截图

EDIT 1 编辑1

Here is inital screen 这是初始屏幕 在此处输入图片说明

then I have items: 然后我有项目:

在此处输入图片说明

When I unfold this 当我展开这个

在此处输入图片说明

And this is at the top layer of object - it has some InnerXml, InnerText... If I want to achieve CoordGeom, there is a lot object.Item(i).ChildNodes.Item(j).ChildNodes... 这是对象的顶层-它具有一些InnerXml,InnerText ...如果我想实现CoordGeom,则有很多object.Item(i).ChildNodes.Item(j).ChildNodes ...

And all of that is because in some lines, lists of objects are made like List as for CoordGeom 所有这些是因为在某些行中,对象列表的生成类似于CoordGeom的List

Because there are multiple allowed types, the Items collection is typed as object. 因为存在多种允许的类型,所以Items集合被键入为对象。 The simplest approach is to enumerate and cast each item: 最简单的方法是枚举并强制转换每个项目:

foreach(var item in coordGeomItems.Items)
{
    var curve = item as Curve;
    if (curve != null)
    {
        // access curve properties here
    }

    var spiral = item as Spiral
    if (spiral != null)
    {
        // access spiral properties here
    }

    // ...
}

You could build up a list of Curves and Spirals and access them using properties with custom getters: 您可以建立“曲线和螺旋”列表,并使用带有自定义吸气剂的属性进行访问:

class CoordGeom
{
    public List<object> Items;

    List<Curve> _curves;
    public List<Curve> Curves
    {
        get 
        { 
            return _curves ?? (_curves = Items
                .Where(item => item is Curve).Select(curve => (Curve)curve).ToList()); 
        }
    }
}

The null coalescing operator (??) will cause the Curves property to set and return the value of _curves as a list of curves if _curves is null. 如果_curves为null,则空合并运算符(??)将导致Curves属性设置并返回_curves的值作为曲线列表。 This basically causes it to initialize the list on the first get and on all subsequent gets it will return the already initialized list. 这基本上使它在第一个get上初始化列表,并在所有后续的get上返回已初始化的列表。

As you cannot change the generated class nor the XML.The best possible approach would be to write an extension method. 由于您无法更改生成的类或XML,因此最好的方法是编写扩展方法。

public static List<Curve> GetCurves(this CoordGeom cg)
{
return cg.Items.OfType<Curve>().ToList();
}

public static List<Spiral> GetSpirals(this CoordGeom cg)
{
return cg.Items.OfType<Spiral>().ToList();
}

Once you do this, you can get items like this 完成此操作后,您可以获得类似的项目

var coordGeomItems = myObject.Alignments.Alignment[0].CoordGeom;
var curves = coordGeomItems.GetCurves();
var spirals = coordGeomItems.GetSpirals();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM