首页 > 解决方案 > 如何反序列化xml父根属性

问题描述

我有以下 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 root(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) 的类结构?

标签: c#.netxmlserialization

解决方案


尝试以下:

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; }
    }
}

推荐阅读