Reputation: 7030
I'd like to use XML Deserializer to parse the following XML structure
<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>
Into something like this that I can use in my application:
[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();
}
}
}
The problem I'm encountering is I have some inception stuff going on in the XML file that I have to deserialize a data_type tag that is inside a data_type tag.
How would one circumvent this issue instead of having to define another class that is differently named?
Upvotes: 1
Views: 208
Reputation: 5503
Add property to your AttributeType
class
[XmlElement("data_type")]
public List<AttributeType> Types { get; set; }
result
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; }
}
Upvotes: 1