I'm writing a C# WinForm application that should read a pre-existant XML file.
I need to parse the XML file and build up a data structure to reflect the XML file content.
I have some experience in XML serialization an thus I tought to use the .NET XML.Serialization features.
I got stuck in a strange XML structure that I'm unable to match within the class (trough attributes, elements and so on):
<sheet number="1" name="/" tstamps="/">
<title_block>
<title>ECC Push-Pull</title>
<company/>
<rev>0.1</rev>
<date>Sat 21 Mar 2015</date>
<source>ecc83-pp.sch</source>
<comment number="1" value=""/>
<comment number="2" value=""/>
<comment number="3" value=""/>
<comment number="4" value=""/>
</title_block>
</sheet>
The 'weird' part of this is the list of comments. I'm used to find such 'repeating' items as a result of serialization of a list/array of elements with XmlArray directive. Using such approach anyway would introduce a surrouding tag to include the list of items. In this case how can I 'reflect' this structure? This's the code that I should use (without the 'missing' comments):
[XmlRoot("sheet")]
public class Sheet
{
[XmlAttribute("number")]
public int Number { get; }
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("tstamps")]
public UInt32 TimeStamps { get; set; }
[XmlElement]
public SheetTitle Title { get; set; }
public Sheet()
{
Title = new SheetTitle();
}
}
[XmlRoot("title_block")]
public class SheetTitle
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("company")]
public string Company { get; set; }
[XmlElement("rev")]
public string Revision { get; set; }
[XmlElement("date")]
public DateTime Date { get; set; }
[XmlElement("source")]
public string Source { get; set; }
public SheetTitle()
{
Date = DateTime.Now;
}
}
[XmlRoot("comment")]
public class Comment
{
[XmlAttribute("number")]
public int Number { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
I have another side question. The best class hierarchy to reflect this structure is: a) nesting the classes in the same way of the xml elements b) keep all classes at the same level (without a hierarchy)? There are side effects to keep into account ?