如果我阅读此 xml:
<?xml version="1.0"?>
<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FontPath>sciezka</FontPath>
<CodingCP852v2>44</CodingCP852v2>
<LedText>Napismoj</LedText>
</Settings>
反序列化将显示在内部异常中:
{"The string '44' is not a valid Boolean value."}
现在我想从 wchich 导致异常的字段的异常对象名称中读取(在这个例子中我应该得到“编码”)。怎么做?
[Serializable]
public class Settings
{
public string FontPath
{
get;
set;
}
public bool Coding
{
get;
set;
}
}
try
{
using (FileStream s = File.OpenRead(fileName))
{
XmlSerializer xs = new XmlSerializer(typeof(Settings));
return (Settings)xs.Deserialize(s);
}
}
catch (Exception ex)
{
return new Settings();
}