I am not sure if deserializing manually is the correct path for your use case. But, if you want a good all-around XML deserializer try this.
EDIT: This is the correct function for returning a collection.
/// <summary>
/// Converts an XDoc into a List<T> of entities
/// </summary>
/// <typeparam name="T">Any serializable object</typeparam>
/// <param name="doc"></param>
/// <returns></returns>
public static List<T> DeserializeCollection<T>(XDocument doc)
{
if (doc == null)
return null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
XmlReader reader = doc.CreateReader();
List<T> result = (List<T>)serializer.Deserialize(reader);
reader.Close();
return result;
}
catch (Exception e)
{
Logging.Log(Severity.Error, "Unable to deserialize document.", e);
return null;
}
}
I am not sure why you have a namespace in the <ArrayOf node. Try removing it. That should work. You can also feed your List into this function to see what the "correct"/"expected" XML should be.
/// <summary>
/// Converts a List<T> of entities into an XDoc.
/// </summary>
/// <typeparam name="T">Any serializable object</typeparam>
/// <param name="doc"></param>
/// <param name="paramList"></param>
public static XDocument SerializeCollection<T>(List<T> paramList)
{
if (paramList == null)
return null;
XDocument doc = new XDocument();
try
{
XmlSerializer serializer = new XmlSerializer(paramList.GetType());
XmlWriter writer = doc.CreateWriter();
serializer.Serialize(writer, paramList);
writer.Close();
return doc;
}
catch (Exception e)
{
Logging.Log(Severity.Error, "Unable to serialize list", e);
return null;
}
}
}