【问题标题】:How to deserialize xml parent root attribute如何反序列化xml父根属性
【发布时间】:2019-05-17 07:19:58
【问题描述】:

我有以下 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# .net xml serialization


    【解决方案1】:

    尝试以下:

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

    【讨论】:

    • Thanks@jdweng.. 但在 FullLoadPerformanceGrid 类中,我必须将“Datatype”定义为“PerformanceType”属性而不是字符串。
    • 这是一个字符串属性。它可能是架构中的枚举。 XML 属性没有子属性。您可能希望有一个指示继承的类型参数。请参阅:docs.microsoft.com/en-us/dotnet/api/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2020-08-28
    相关资源
    最近更新 更多