如何反序列化xml父根属性

时间:2019-05-17 07:19:58

标签: c# .net xml serialization

我具有以下XML结构:

<ComputationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DataType="DimX">
  <Mode Name="M1">
    <Area>
      <Point PointNumber="3" Speed="127" Power="1455" Value="-2" />
      <Point PointNumber="2" Speed="127" Power="1396.8" Value="2" />
      <Point PointNumber="3" Speed="101.6" Power="1164" Value="-2" />
    </Area> 
  </Mode>
</ComputationData>

在下面的类结构中,我无法获取XMl根(ComputationData)中可用的Datatype的值。

对于以下XML,我创建了以下类结构:

[Serializable]
[XmlRoot("ComputationData")]
public class FullLoadPerformanceGrid
{
    /// <summary>
    /// Name of the performance data type of this grid, e.g. Bsfc, tEat...
    /// </summary>
    [XmlElement(ElementName = "ComputationData", Type = 
    typeof(PerformanceType))]
    public PerformanceType DataType { get; set; }

    /// <summary>
    /// List of available <see cref="FullLoadPerformanceMode"/> 
    /// </summary>
    [XmlElement("Mode", Type = typeof(FullLoadPerformanceMode))]
    public List<FullLoadPerformanceMode> Modes { get; set; }
}

[Serializable]
[XmlRoot("ComputationData")]
public class PerformanceType
{        
    public int Id { get; set; }

    [DataMember(Name = "DataType")]
    [XmlAttribute("DataType")]
    public string DataType { get; set; }     

    public int Type { get; set; }
}

有人可以帮助我定义如何定义PerformanceType(DataType)的类结构吗?

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication3
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(FullLoadPerformanceGrid));

            FullLoadPerformanceGrid load = (FullLoadPerformanceGrid)serializer.Deserialize(reader);
        }
    }
    [XmlRoot(ElementName = "ComputationData", Namespace = "")] 
    public class FullLoadPerformanceGrid
    {

        [XmlAttribute("DataType", Namespace = "")]
        public string DataType { get; set; }

        [XmlElement(ElementName = "Mode", Namespace = "")]
        public Mode mode { get; set; }

    }
    [XmlRoot(ElementName = "Mode", Namespace = "" )]
    public class Mode
    {
        [XmlAttribute("Name", Namespace = "")]
        public string name { get; set; }

        [XmlArray("Area", Namespace = "")]
        [XmlArrayItem("Point", Namespace = "")]
        public List<Point> points { get; set; }

    }

    [XmlRoot(ElementName = "Point", Namespace = "")]
    public class Point
    {
        [XmlAttribute("PointNumber", Namespace = "")]
        public int pointNumber { get; set; }

        [XmlAttribute("Speed", Namespace = "")]
        public decimal speed { get; set; }

        [XmlAttribute("Power", Namespace = "")]
        public decimal power { get; set; }

        [XmlAttribute("Value", Namespace = "")]
        public int value { get; set; }
    }
}