2012-03-15 57 views
4

我想分析一些trx文件(webTestResults)來輸出合成的Excel文件。TestRunType的序列化拋出異常

首先,我將trx xsd模式(Visual Studio目錄中的vstst.xsd)生成一堆C#類。

然後,我嘗試基於TestRunType的類型(從模式生成)實例化XmlSerializer。

XmlSerializer xmlSer = new XmlSerializer(typeof(TestRunType)); 

XmlSerializer的實例引發了一個異常:

System.InvalidOperationException: There was an error reflecting type 'TestRunType'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TestRunTypeTestDefinitions'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'OrderedTestType'. ---> System.InvalidOperationException: There was an error reflecting type 'CodedWebTestElementType'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: Member 'CodedWebTestElementType.Items' hides inherited member 'BaseTestType.Items', but has different custom attributes. 
    at System.Xml.Serialization.StructMapping.FindDeclaringMapping(MemberMapping member, StructMapping& declaringMapping, String parent) 
    at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) 

什麼是自定義屬性? 只是BaseTestType的開頭:

public abstract partial class BaseTestType { 

    private object[] itemsField; 

    private bool enabledField; 

    private string idField; 

    private string nameField; 

    private bool isGroupableField; 

    private int priorityField; 

    private string namedCategoryField; 

    private string storageField; 

    public BaseTestType() { 
     this.enabledField = true; 
     this.isGroupableField = true; 
     this.priorityField = 2147483647; 
     this.namedCategoryField = ""; 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Agent", typeof(BaseTestTypeAgent))] 
    [System.Xml.Serialization.XmlElementAttribute("Css", typeof(BaseTestTypeCss))] 
    [System.Xml.Serialization.XmlElementAttribute("DeploymentItems", typeof(BaseTestTypeDeploymentItems))] 
    [System.Xml.Serialization.XmlElementAttribute("Description", typeof(object))] 
    [System.Xml.Serialization.XmlElementAttribute("Execution", typeof(BaseTestTypeExecution))] 
    [System.Xml.Serialization.XmlElementAttribute("Owners", typeof(BaseTestTypeOwners))] 
    [System.Xml.Serialization.XmlElementAttribute("Properties", typeof(BaseTestTypeProperties))] 
    [System.Xml.Serialization.XmlElementAttribute("TcmInformation", typeof(TcmInformationType))] 
    [System.Xml.Serialization.XmlElementAttribute("TestCategory", typeof(BaseTestTypeTestCategory))] 
    [System.Xml.Serialization.XmlElementAttribute("WorkItemIDs", typeof(WorkItemIDsType))] 
    public object[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 

而且CodedWebTestElementType:

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010")] 
public partial class CodedWebTestElementType : BaseTestType { 

    private object[] itemsField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("IncludedWebTests", typeof(CodedWebTestElementTypeIncludedWebTests))] 
    [System.Xml.Serialization.XmlElementAttribute("WebTestClass", typeof(CodedWebTestElementTypeWebTestClass))] 
    public object[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 
} 

有人能向我解釋的問題是什麼?

感謝...

回答

8

的解決方案是從派生類(CodedWebTestElementTypeGenericTestType)刪除項目屬性,也給移動序列化屬性的基類,以免錯過萬一你值有一個編碼的網絡測試或通用測試。

IOW,解決方案如下。

首先,從CodedWebTestElementType類型刪除Items財產

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010")] 
public partial class CodedWebTestElementType : BaseTestType { 
} 

然後移動它的兩位XmlElementAttribute屬性的BaseTestType基類(見最後兩個):

/// <remarks/> 
[System.Xml.Serialization.XmlElementAttribute("Agent", typeof(BaseTestTypeAgent))] 
[System.Xml.Serialization.XmlElementAttribute("Css", typeof(BaseTestTypeCss))] 
[System.Xml.Serialization.XmlElementAttribute("DeploymentItems", typeof(BaseTestTypeDeploymentItems))] 
[System.Xml.Serialization.XmlElementAttribute("Description", typeof(object))] 
[System.Xml.Serialization.XmlElementAttribute("Execution", typeof(BaseTestTypeExecution))] 
[System.Xml.Serialization.XmlElementAttribute("Owners", typeof(BaseTestTypeOwners))] 
[System.Xml.Serialization.XmlElementAttribute("Properties", typeof(BaseTestTypeProperties))] 
[System.Xml.Serialization.XmlElementAttribute("TcmInformation", typeof(TcmInformationType))] 
[System.Xml.Serialization.XmlElementAttribute("TestCategory", typeof(BaseTestTypeTestCategory))] 
[System.Xml.Serialization.XmlElementAttribute("WorkItemIDs", typeof(WorkItemIDsType))] 
[System.Xml.Serialization.XmlElementAttribute("IncludedWebTests", typeof(CodedWebTestElementTypeIncludedWebTests))] 
[System.Xml.Serialization.XmlElementAttribute("WebTestClass", typeof(CodedWebTestElementTypeWebTestClass))] 
public object[] Items 
{ 
    get { 
     return this.itemsField; 
    } 
    set { 
     this.itemsField = value; 
    } 
} 

之後,做對於GenericTestType類同樣的事情。

這樣你就不會丟失情況下的信息你IncludedWebTestsWebTestClass命令SummaryXmlFile1天節點。