I'm trying to deserialize a XML file which Looks like the following:
<xml>
<classes>
<class name="EventLog">
<attribute name="TYPE" type="int"></attribute>
<attribute name="DESCRIPTION" type="string"></attribute>
<attribute name="ISSUEDBY" type="string></attribute>
<attribute name="DATE" type="hr_clock"></attribute>
</class>
<class name="test">
<attribute name="ttt" type="int"></attribute>
<attribute name="ppp" type="string"></attribute>
<attribute name="xxx" type="string"></attribute>
<attribute name="aaa" type="hr_clock"></attribute>
</class>
</classes>
<filters>
<filter name="COILORDERFILTER">
<attribute name="COILID" type="string"></attribute>
<attribute name="RELIABID" type="string"></attribute>
</filter>
<filter name="DriveDataFilter">
<attribute name="DRIVEID" type="string"></attribute>
</filter>
</filters>
</xml>
I need only the classes between the nodes classes
. I created following classes for the deserialization:
[Serializable()]
[System.Xml.Serialization.XmlRoot("classes")]
public class ClassCollection
{
[XmlArray("class")]
[XmlArrayItem("attribute", typeof(SingleClass))]
public SingleClass[] singleClass { get; set; }
}
[Serializable()]
public class SingleClass
{
[System.Xml.Serialization.XmlAttribute("name")]
public string name { get; set; }
[System.Xml.Serialization.XmlAttribute("type")]
public string type { get; set; }
}
class Program
{
static void Main(string[] args)
{
ClassCollection classes = null;
string path = @"C:\Users\test\Desktop\Eventlog.xml";
XmlSerializer serializer = new XmlSerializer(typeof(ClassCollection));
StreamReader reader = new StreamReader(path);
try
{
classes = (ClassCollection)serializer.Deserialize(reader);
}
catch (InvalidOperationException excep)
{
Console.WriteLine(excep.ToString());
}
Console.Read();
}
}
Can anybody say me "what is wrong"?