public class Warning
{
public Warning()
{
}
public Warning(String name, List<String> conflictList)
{
Name = name;
ConflictList = conflictList;
}
public string Name
{
get;
set;
}
public List<String> ConflictList
{
get;
set;
}
public static Warning ReadXML(string xml)
{
Warning warning = null;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Warning));
using (StringReader sr = new StringReader(xml))
{
XmlTextReader xtr = new XmlTextReader(sr);
warning = (Warning)serializer.Deserialize(xtr);
xtr.Close();
sr.Close();
}
}
catch
{
}
return warning;
}
public void Save(string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(Warning));
using (StreamWriter writer = new StreamWriter(filename))
{
serializer.Serialize(writer, this);
writer.Close();
}
}
}
If I deserializing the following xml string and I get non-null values back for both Name and ConflictList. This is fine and what I expect.
Warning w1 = Warning.ReadXML(
"<Warning><Name>test warning</Name><ConflictList><string>file1.txt</string></ConflictList></Warning>");
w1.Name returns "test warning" and w1.ConflictList returns a list containing the text "file1.txt"
However, if I deserialize the following xml string, w2.Name returns null, which i understand, but ConflictList is actually a list with a Count of 0. I expected it to be null as well. How come?
Warning w2 = Warning.ReadXML("<Warning></Warning>");
I am sure in some cases w1.ConflictList can return null if the element is not present in the xml string, but when does this happen?