locked
Problem with XmlSerializer - "There was an error reflecting type.."

    Question

  • i'm working .NetFramework4 Client Profile / Visual Studio 2010 Ultimate / Windows7.

    I want to read some xx.trx files. TRX file is Test Result File of Microsoft Unit Test Framework.

    Generated runtime classes from an XSD schema file using by 'XML Schema Definition Tool(Xsd.exe)' for Microsoft Unit Test Result File( XX.TRX) and Schema File(vstst.xsd).

    And programmmed this code.

            public void ReadTrxFileContents(string filePath)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TestRunType));    // But exception occured 'There was an error reflecting type..'
                TestRunType xmlTestRunType = serializer.Deserialize(new StreamReader(filePath)) as TestRunType;

                // add custom code
                Debug.WriteLine("id : " + xmlTestRunType.id + "\n");
                Debug.WriteLine("name : " + xmlTestRunType.name + "\n");
                Debug.WriteLine("runuser : " + xmlTestRunType.runUser + "\n");
            }

     

    Why occured exception ?

    XmlSerializer serializer = new XmlSerializer(typeof(TestRunType));    // But exception occured 'There was an error reflecting type..'

     

    I added also a parameterless constructor.
    TestRunType Definition is ...

    /// <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")]
    [System.Xml.Serialization.XmlRootAttribute("TestRun", Namespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010", IsNullable=false)]
    public partial class TestRunType {
        // add a parameterless constructor
        public TestRunType() { }

        private object[] itemsField;
       
        private string idField;
       
        private string nameField;
       
        private string runUserField;
       
        private int tcmPassIdField;
       
        private bool tcmPassIdFieldSpecified;
       
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Build", typeof(TestRunTypeBuild))]
        [System.Xml.Serialization.XmlElementAttribute("ResultSummary", typeof(TestRunTypeResultSummary))]
        [System.Xml.Serialization.XmlElementAttribute("Results", typeof(ResultsType))]
        [System.Xml.Serialization.XmlElementAttribute("TestDefinitions", typeof(TestRunTypeTestDefinitions))]
        [System.Xml.Serialization.XmlElementAttribute("TestEntries", typeof(TestEntriesType1))]
        [System.Xml.Serialization.XmlElementAttribute("TestLists", typeof(TestRunTypeTestLists))]
        [System.Xml.Serialization.XmlElementAttribute("TestRunConfiguration", typeof(TestRunConfiguration))]
        [System.Xml.Serialization.XmlElementAttribute("TestSettings", typeof(TestSettingsType))]
        [System.Xml.Serialization.XmlElementAttribute("Times", typeof(TestRunTypeTimes))]
        [System.Xml.Serialization.XmlElementAttribute("UserData", typeof(System.Xml.XmlElement))]
        public object[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id {
            get {
                return this.idField;
            }
            set {
                this.idField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string runUser {
            get {
                return this.runUserField;
            }
            set {
                this.runUserField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public int tcmPassId {
            get {
                return this.tcmPassIdField;
            }
            set {
                this.tcmPassIdField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool tcmPassIdSpecified {
            get {
                return this.tcmPassIdFieldSpecified;
            }
            set {
                this.tcmPassIdFieldSpecified = value;
            }
        }
    }

    Monday, January 17, 2011 4:05 AM

Answers

  • check the inner exception for more details
    http://webservices20.blogspot.com/
    WCF Security, Interoperability And Performance Blog
    • Marked as answer by canyou Thursday, January 20, 2011 12:49 AM
    Monday, January 17, 2011 6:10 PM
  • Thanks All.

     

    I catch some bugs.

    I checked the inner exception for more details. And Last Bug message is...

    'There was an error reflecting type 'CodedWebTestElementType' , Member 'CodedWebTestElementType.Items' hides inherited member 'BaseTestType.Items', but has different custom attributes.

     

     So i kill some code. 

    1) 'Items' of CodedWebTestElementType class

    2) 'Items' of GenericTestType class

     

    And then program is good runnned !! I can read 'trx'. Thanks.


    • Marked as answer by canyou Thursday, January 20, 2011 12:49 AM
    Wednesday, January 19, 2011 8:05 AM

All replies

  • check the inner exception for more details
    http://webservices20.blogspot.com/
    WCF Security, Interoperability And Performance Blog
    • Marked as answer by canyou Thursday, January 20, 2011 12:49 AM
    Monday, January 17, 2011 6:10 PM
  • What's mean ?

    Problem is...

    XmlSerializer serializer = new XmlSerializer(typeof(TestRunType));    // exception occured 'There was an error reflecting type TestRunType'

    Other Type is no problem.

     

    Monday, January 17, 2011 11:13 PM
  • i'm working .NetFramework4 Client Profile / Visual Studio 2010 Ultimate / Windows7.

    I want to read some xx.trx files. TRX file is Test Result File of Microsoft Unit Test Framework.

    Generated runtime classes from an XSD schema file using by 'XML Schema Definition Tool(Xsd.exe)' for Microsoft Unit Test Result File( XX.TRX) and Schema File(vstst.xsd).

    And programmming code is..

            public void ReadTrxFileContents(string filePath)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TestRunType));    // But exception occured 'There was an error reflecting type..'
                TestRunType xmlTestRunType = serializer.Deserialize(new StreamReader(filePath)) as TestRunType;

                // add custom code
                Debug.WriteLine("id : " + xmlTestRunType.id + "\n");
                Debug.WriteLine("name : " + xmlTestRunType.name + "\n");
                Debug.WriteLine("runuser : " + xmlTestRunType.runUser + "\n");
            }

     

    Why occured exception ?

    XmlSerializer serializer = new XmlSerializer(typeof(TestRunType));    // But exception occured 'There was an error reflecting type TestRunType'

     

    I added also a parameterless constructor.
    TestRunType Definition is ...

    /// <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")]
    [System.Xml.Serialization.XmlRootAttribute("TestRun", Namespace="http://microsoft.com/schemas/VisualStudio/TeamTest/2010", IsNullable=false)]
    public partial class TestRunType {
        // add a parameterless constructor
        public TestRunType() { }

        private object[] itemsField;
       
        private string idField;
       
        private string nameField;
       
        private string runUserField;
       
        private int tcmPassIdField;
       
        private bool tcmPassIdFieldSpecified;
       
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Build", typeof(TestRunTypeBuild))]
        [System.Xml.Serialization.XmlElementAttribute("ResultSummary", typeof(TestRunTypeResultSummary))]
        [System.Xml.Serialization.XmlElementAttribute("Results", typeof(ResultsType))]
        [System.Xml.Serialization.XmlElementAttribute("TestDefinitions", typeof(TestRunTypeTestDefinitions))]
        [System.Xml.Serialization.XmlElementAttribute("TestEntries", typeof(TestEntriesType1))]
        [System.Xml.Serialization.XmlElementAttribute("TestLists", typeof(TestRunTypeTestLists))]
        [System.Xml.Serialization.XmlElementAttribute("TestRunConfiguration", typeof(TestRunConfiguration))]
        [System.Xml.Serialization.XmlElementAttribute("TestSettings", typeof(TestSettingsType))]
        [System.Xml.Serialization.XmlElementAttribute("Times", typeof(TestRunTypeTimes))]
        [System.Xml.Serialization.XmlElementAttribute("UserData", typeof(System.Xml.XmlElement))]
        public object[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string id {
            get {
                return this.idField;
            }
            set {
                this.idField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string runUser {
            get {
                return this.runUserField;
            }
            set {
                this.runUserField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public int tcmPassId {
            get {
                return this.tcmPassIdField;
            }
            set {
                this.tcmPassIdField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool tcmPassIdSpecified {
            get {
                return this.tcmPassIdFieldSpecified;
            }
            set {
                this.tcmPassIdFieldSpecified = value;
            }
        }
    }

    • Merged by Shane_Hu Tuesday, January 25, 2011 6:39 AM
    Monday, January 17, 2011 11:32 PM
  • No. Catch the exception and then display the entire exception:

    try
    {
      XmlSerializer serializer = new XmlSerializer(typeof(TestRunType));
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.ToString()); // Or however you get it to display
    }
    


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Wednesday, January 19, 2011 12:22 AM
    Moderator
  • Thanks All.

     

    I catch some bugs.

    I checked the inner exception for more details. And Last Bug message is...

    'There was an error reflecting type 'CodedWebTestElementType' , Member 'CodedWebTestElementType.Items' hides inherited member 'BaseTestType.Items', but has different custom attributes.

     

     So i kill some code. 

    1) 'Items' of CodedWebTestElementType class

    2) 'Items' of GenericTestType class

     

    And then program is good runnned !! I can read 'trx'. Thanks.


    • Marked as answer by canyou Thursday, January 20, 2011 12:49 AM
    Wednesday, January 19, 2011 8:05 AM
  •  Try this. It should work

    public void ReadTrxFileContents(string filePath)
    {

    TestRunType xmlTestRunType; 
    XmlSerializer serializer = new XmlSerializer(typeof(TestRunType)); 
    using (TextReader textReader = new StreamReader(filePath)) 
    { 
         xmlTestRunType
    = serializer.Deserialize(textReader) as TestRunType; 
    } 

    .....
    }

    Monday, January 24, 2011 2:29 PM
  •  Try this. It should work

    public void ReadTrxFileContents(string filePath)
    {

    TestRunType xmlTestRunType; 
    XmlSerializer serializer = new XmlSerializer(typeof(TestRunType)); 
    using (TextReader textReader = new StreamReader(filePath)) 
    { 
         xmlTestRunType
    = serializer.Deserialize(textReader) as TestRunType; 
    } 

    .....
    }

    Try with generic List<> instead object[].
    Monday, January 24, 2011 2:42 PM
  • Robert,

     

    I am getting same error even with your code

    The change you suggested to use List<> I am not sure because its an xsd.exe generated code and we have many places object[] returned.

     

    Also, what should I define as type in List<>?

    If I remove the type definitions as mentioned by canyou, it works... not sure why...

     

    Please advise

     

    Wednesday, February 2, 2011 6:40 PM
  • If you kill this code, some of the trx files will not be parsed completely.

    The solution is that to kill that code, BUT at the same moment, to move the properties attributes to the base class.

    See the details here: Serialization of TestRunType throwing an exception

    Sunday, June 3, 2012 6:54 PM