2013-04-29 30 views
0

我必須反序列化XML。我有一個XSD來控制可用標籤。當我犯了一些錯誤,並且XML包含不規則標籤時,XmlSerializer不會給出錯誤,而是跳過不規則標籤。錯誤的XML不會給連接XSD時的解析錯誤

我該如何強制運行時錯誤?

這裏是我的課的一部分:

[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是有效的,僅包含來自架構元素。

回答