Hi all,
I have an XML file that is created by a powershell script, each XML has differing amounts of nodes within it. An example of a shorter one is below:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>...</T>
<T>...</T>
<T>...</T>
</TN>
<MS>
<S N="LevelDisplayName">...</S>
<S N="Message">...</S>
<S N="MachineName">...</S>
<I32 N="Id">...</I32>
</MS>
</Obj>
<Obj RefId="1">
<TNRef RefId="0" />
<MS>
<S N="LevelDisplayName">...</S>
<S N="Message">...</S>
<S N="MachineName">...</S>
<I32 N="Id">...</I32>
</MS>
</Obj>
</Objs>
Now i want to get this XML file and convert it into a useable C# object. I've tried using a deserializer method but as the amount of nodes and what they are tagged as can change, i cannot create a class that accomodates these deserialized variables, so will error everytime.
public static List<T> RetrieveFromXML<T>(string filename, string root)
{
List<T> t;
using (FileStream fs = new FileStream(filename, FileMode.Open))
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(root));
t = (List<T>)ser.Deserialize(fs);
}
return t;
}
Is there a way that an object can be dynamically created from the XML, or any other method that you guys have come across?