简体   繁体   中英

How to deserialize xml parent root attribute

I have following XML Structure:

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

In below class structure I am not able to get the value of Datatype which is available in XMl root(ComputationData).

For following XML I have created following class structure:

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

Can anyone help me with the class structure that how should I define the PerformanceType(DataType)?

Try following :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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