0

What is the best practice method of reading an xml file to instance a collection of a .cs object generated via xsd.exe?

I used xsd.exe to generate an .xsd file (schema) and then to generate a .cs file. I followed the steps here: http://ctrlf5.net/?p=235 and it worked great.

I don't want to use XmlReader and write a bunch of code to navigate the document tree, setting all the public setters along the way. My xml document is long and painful. I just want to hit the easy button and have my collection. The newbie that I am just had a awesome moment with xsd - how cool it made my .cs file - but if I now have to write 500 lines of code to instance my class - not awesome. It only makes sense that there is an easy way to now instance my collection and I just don't know what it is or how to google it. Post back to this question already answered gladly accepted.

Here is what my xml and code looks like:

<?xml version = "1.0" ?>
<MY_OBJECT>
  <UNIQUE_ID>ABC</UNIQUE_ID>
  <TYPE>TEST</TYPE>
  <CLASSALIST>
    <CLASSA>
      <A>0</A>
      <B>0</B>
      <C>2598960</C>
      <HS>
        <H>
          <DESCRIPTION>MYDESC</DESCRIPTION>
          <ADDITIONAL>0</ADDITIONAL>
        </H>
      </HS>
    </CLASSA>
  </CLASSALIST>
  <BONUSES>
    <BONUS>
      <BONUS_TYPE>Bonus Schedule</BONUS_TYPE>
      <BONUS_DATA>
        <ALPHA>1</ALPHA>
        <BETA>4</BETA>        
      </BONUS_DATA>
    </BONUS>
  </BONUSES>
  <REVISION>A</REVISION>
  <CONDITION>
    <GENERAL></GENERAL>
    <EXCEPTION></EXCEPTION>
  </CONDITION>
  <ACTIONABLE>True</ACTIONABLE>
  <VERSION>12345</VERSION>
  <COMMENTS></COMMENTS>
</MY_OBJECT>

Here i try to deserialize to List<>:

[Test]
public void AutoXmlSampleList()
{
    var xs = new XmlSerializer(typeof(List<MY_OBJECT>));
    List<MY_OBJECT> list;
    using (var reader = XmlReader.Create(_inputFilename2))
    {
        list = (List<MY_OBJECT>)xs.Deserialize(reader);
    }

    Assert.AreEqual("ABC", list[0].UNIQUE_ID);

}

Here is the error message:

System.InvalidOperationException : Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Test.MY_OBJECTCLASSALISTCLASSA[]' to 'Test.MY_OBJECTCLASSALISTCLASSA'
error CS0030: Cannot convert type 'Test.MY_OBJECTBONUSESBONUS[]' to 'Test.MY_OBJECTBONUSESBONUS'
error CS0030: Cannot convert type 'Test.MY_OBJECTCLASSALISTCLASSAHSH[]' to 'Test.MY_OBJECTCLASSALISTCLASSAHSH'
error CS0029: Cannot implicitly convert type 'Test.MY_OBJECTCLASSALISTCLASSA' to 'Test.MY_OBJECTCLASSALISTCLASSA[]'
error CS0029: Cannot implicitly convert type 'Test.MY_OBJECTBONUSESBONUS' to 'Test.MY_OBJECTBONUSESBONUS[]'
error CS0029: Cannot implicitly convert type 'Test.MY_OBJECTCLASSALISTCLASSAHSH' to 'Test.MY_OBJECTCLASSALISTCLASSAHSH[]'

and if I try deserialize not List<>:

[Test]
public void AutoXmlSampleNotList()
{
    var xs = new XmlSerializer(typeof(MY_OBJECT));
    MY_OBJECT myObject;
    using (var reader = XmlReader.Create(_inputFilename2))
    {
        myObject = (MY_OBJECT)xs.Deserialize(reader);
    }

    Assert.AreEqual("ABC", myObject.UNIQUE_ID);

}

which results in a similar error:

System.InvalidOperationException : Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Test.MY_OBJECTCLASSALISTCLASSA[]' to 'Test.MY_OBJECTCLASSALISTCLASSA'
error CS0030: Cannot convert type 'Test.MY_OBJECTBONUSESBONUS[]' to 'Test.MY_OBJECTBONUSESBONUS'
error CS0030: Cannot convert type 'Test.MY_OBJECTCLASSALISTCLASSAHSH[]' to 'Test.MY_OBJECTCLASSALISTCLASSAHSH'
error CS0029: Cannot implicitly convert type 'Test.MY_OBJECTCLASSALISTCLASSA' to 'Test.MY_OBJECTCLASSALISTCLASSA[]'
error CS0029: Cannot implicitly convert type 'Test.MY_OBJECTBONUSESBONUS' to 'Test.MY_OBJECTBONUSESBONUS[]'
error CS0029: Cannot implicitly convert type 'Test.MY_OBJECTCLASSALISTCLASSAHSH' to 'Test.MY_OBJECTCLASSALISTCLASSAHSH[]'
sapbucket
  • 6,795
  • 15
  • 57
  • 94

1 Answers1

0
var xs = new XmlSerializer(typeof(List<YourClassGeneratedByXsd>));
List<YourClassGeneratedByXsd> list;
using (var reader = XmlReader.Create(fileName))
{
    list = (List<YourClassGeneratedByXsd>)xs.Deserialize(reader);
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Thomas, I get the same error message (error CS0030, etc). Could it be that the autogenerated *.cs requires some manual edits? – sapbucket Nov 02 '11 at 17:00
  • @sapbucket, no idea... xsd.exe doesn't support every feature of XML Schema, perhaps your XSD uses some unsupported features – Thomas Levesque Nov 02 '11 at 17:07
  • Why a `List` and not just `T`? XML only has one root object as far as I remember. – leppie Nov 02 '11 at 17:43
  • @leppie, the OP said he wanted to "instance a collection of a .cs object", so I assumed that the XML contained a collection. But now that the XML has been posted, it seems to be a single object... – Thomas Levesque Nov 02 '11 at 23:19