我必须反序列化 XML。我有一个 XSD 来控制可用的标签。当我犯了一些错误并且 XML 包含不规则标签时,XmlSerializer 不会给出错误,而是跳过不规则标签。
我如何能够强制运行时错误?
这是我的课的一部分:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/BXTestScript.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/BXTestScript.xsd", IsNullable=false)]
public partial class BXTestScript {
[System.Xml.Serialization.XmlElementAttribute("ActivateMenu", typeof(BXTestScriptActivateMenu))]
[System.Xml.Serialization.XmlElementAttribute("BreakPoint", typeof(BXTestScriptBreakPoint))]
...
public static BXTestScript ReadXml(string path) {
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(BXTestScript));
using (System.IO.StreamReader sr = new System.IO.StreamReader(path))
BXTestScript ts = (BXTestScript)ser.Deserialize(sr);
return ts;
}
...
}
如果 XML 有效并且仅包含模式中的元素,则所有程序都可以正常工作。