Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization of TestRunType throwing an exception [duplicate]

I'm trying to analyse some trx files (webTestResults) to output synthetic excel files.

first, I take the trx xsd schema (vstst.xsd in visual studio directory) to generate a bunch of C# Classes.

then, I try to instantiate an XmlSerializer based on the type of TestRunType (generated from the schema).

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

The XMLSerializer instantiation raises an exception :

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)

what are the custom attributes ? Just the beginning of 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;
        }
    }

And 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;
        }
    }
}

Could someone explain to me what the problem is ?

thanks...

like image 639
LB40 Avatar asked Mar 15 '12 14:03

LB40


1 Answers

The solution is to remove Items property from the derived classes (CodedWebTestElementType and GenericTestType), BUT also to move the serialization attributes to the base class in order not to miss the values in case you have a coded web test or a generic test.

IOW, the solution is the following.

First, remove Items property from CodedWebTestElementType type

/// <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 {
}

Then, move it's two XmlElementAttribute attributes to the BaseTestType base class (see the last two):

/// <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;
    }
}

After that, do the same thing for the GenericTestType class.

This way you will not lose the information in case you get IncludedWebTests, WebTestClass, Command or SummaryXmlFile nodes one day.

like image 163
Nikita R. Avatar answered Sep 20 '22 22:09

Nikita R.