使用Serializer解析XML结构

时间:2012-08-16 06:06:28

标签: c# .net wpf xml serialization

我想使用XML Deserializer来解析以下XML结构

  <function_type start_key="02" key_index_reverse="true" key_index="1" key_index_length="1" key="03" name ="N/A" id="N/A" transmission="rx">
    <data_type size="1">STX</data_type>
    <data_type size="1">Respond</data_type>
    <data_type size="2" unbounded="true" end="03">Function Code
      <convert>30,IND RESPOND</convert>
      <convert>31,GRP RESPOND</convert>
      <convert>32,PATTERN RESPOND</convert>
      <convert>33,F PATTERN RESPOND</convert>
      <convert>34,DIMMING RESPOND</convert>
      <data_type size="2" address="true">ADDRESS</data_type>
      <data_type size="4" decimal="true">CIRCUIT NUMBER</data_type>
      <data_type size="1">Value
        <convert>30,OFF</convert>
        <convert>31,ON</convert>
      </data_type>
    </data_type>

我可以在我的应用程序中使用这样的东西:

[XmlRoot("function_type")]
public class FunctionType
{
    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlAttribute("id")]
    public string ID { get; set; }

    [XmlAttribute("size_total")]
    public int TotalSize { get; set; }

    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlAttribute("transmission")]
    public string Transmission { get; set; }

    [XmlElement("data_type")]
    public List<AttributeType> Types { get; set; }

    [XmlAttribute("key_index")]
    public int KeyIndex { get; set; }

    [XmlAttribute("key_index_length")]
    public int KeyIndexLength { get; set; }

    [XmlAttribute("key_index_reverse")]
    public bool KeyIndexReverse { get; set; }

    public FunctionType()
    {
        Types = new List<AttributeType>();
    }
}

public class AttributeType
{
    [XmlAttribute("size")]
    public int Size { get; set; }

    [XmlAttribute("decimal")]
    public bool IsDecimal { get; set; }

    [XmlAttribute("start_key")]
    public string StartKey { get; set; }

    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlAttribute("address")]
    public bool IsAddress { get; set; }

    [XmlAttribute("unbounded")]
    public bool IsUnbounded { get; set; }

    [XmlAttribute("end_key")]
    public int EndKey{ get; set; }

    [XmlElement("convert")]
    public List<ConversionType> Converts { get; set; }

    [XmlText]
    public string Value { get; set; }
}

public class ConversionType
{
    [XmlText]
    public string ConvertValue { get; set; }
}
public static class StringExtensions
{
    public static T XmlDeserialize<T>(this string s)
    {
        var locker = new object();
        var stringReader = new StringReader(s);
        var reader = new XmlTextReader(stringReader);
        try
        {
            var xmlSerializer = new XmlSerializer(typeof(T));
            lock (locker)
            {
                var item = (T)xmlSerializer.Deserialize(reader);
                reader.Close();
                return item;
            }
        }
        catch
        {
            return default(T);
        }
        finally
        {
            reader.Close();
        }
    }
}

我遇到的问题是我在XML文件中有一些初始化的东西,我必须反序列化data_type标记内的data_type标记。

如何绕过这个问题而不必定义另一个名称不同的类?

1 个答案:

答案 0 :(得分:1)

将属性添加到AttributeType

[XmlElement("data_type")]
public List<AttributeType> Types { get; set; }

结果

public class AttributeType
{
    [XmlAttribute("size")]
    public int Size { get; set; }

    [XmlAttribute("decimal")]
    public bool IsDecimal { get; set; }

    [XmlAttribute("start_key")]
    public string StartKey { get; set; }

    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlAttribute("address")]
    public bool IsAddress { get; set; }

    [XmlAttribute("unbounded")]
    public bool IsUnbounded { get; set; }

    [XmlAttribute("end_key")]
    public int EndKey { get; set; }

    [XmlElement("convert")]
    public List<ConversionType> Converts { get; set; }

    [XmlElement("data_type")]
    public List<AttributeType> Types { get; set; }

    [XmlText]
    public string Value { get; set; }
}