I've been using the System.Xml.Serialization.XmlSerializer
and StreamReader
with success for a while now and I've been able to convert Xml into c# class without a problem. But I've found a different kind of Xml file and I can not find any example on how to parse it.
Usually, the Xml files that I find are something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Signal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<variable>
<SignalName>refreshSettings</SignalName>
<Type>X</Type>
<Address>1</Address>
<DefaultValue>0</DefaultValue>
<Function>Settings, General</Function>
<Version>3.0</Version>
<Description>Refresh Page</Description>
</variable>
But now, I found a xml that I need to parse that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configData format="Users" version="2016">
<Object type="Users">
<Attribute name="Comment"></Attribute>
<Attribute name="EnableSending">Yes</Attribute>
<Attribute name="MinPasswordLength">1</Attribute>
<Attribute name="MinDigitsInPassword">0</Attribute>
<Object type="UsersFolder">
<Attribute name="Name">Users</Attribute>
<Attribute name="Comment">This folder contains the configured users.</Attribute>
<Object type="User">
<Attribute name="Name">admin</Attribute>
<Attribute name="Title">Admin User</Attribute>
<Attribute name="UserDescription">User with access level Admin.</Attribute>
<Attribute name="Comment">User for testing to log in with access level Admin. </Attribute>
<Attribute name="Password">123</Attribute>
<Attribute name="TimeOut">01:00:00</Attribute>
<Attribute name="AccessLevel">Admin</Attribute>
</Object>
Any idea on how I could parse this?
I tried something like this:
ConfigData.cs:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class ConfigData
{
[System.Xml.Serialization.XmlElementAttribute("Users")]
public Users[] Users { get; set; }
}
Users.cs:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Users
{
public string Comment { get; set; }
public string EnableAlarmSending { get; set; }
public string MinPasswordLength { get; set; }
public string MinDigitsInPassword { get; set; }
[System.Xml.Serialization.XmlElementAttribute("UsersFolder")]
public UsersFolder[] usersFolder { get; set; }
[System.Xml.Serialization.XmlElementAttribute("GroupsFolder")]
public GroupsFolder[] groupsFolder { get; set; }
}
UsersFolder.cs:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class UsersFolder
{
public string Name { get; set; }
public string Comment { get; set; }
[System.Xml.Serialization.XmlElementAttribute("User")]
public User[] User { get; set; }
}
etc.
and then in some logic class file:
public void readDataFromXML(ref ConfigData data, string filepath)
{
try
{
System.Xml.Serialization.XmlSerializer reader1 = new System.Xml.Serialization.XmlSerializer(data.GetType()); /* Read the XML file. */
StreamReader file1 = new StreamReader(filepath); /* Deserialize the content of the file into a Book object. */
data = (ConfigData)reader1.Deserialize(file1); /* Temporario deve carregar na classe correcta */
file1.Close();
}
catch (Exception e)
{
Console.WriteLine("Error Parsing Data From XML");
throw e;
}
}
called by:
public string buildAreasString()
{
ConfigData data = new ConfigData();
readDataFromXML(ref data, UsersXmlPath);
(...)
return areasString;
}
which gives me the data object with a null Users. Any help would be much appreciated.
Thanks in advance