简体   繁体   English

xsd.exe新手-我​​现在创建了.cs和.xsd,我该如何读取xml?

[英]xsd.exe newbie - I created my .cs and .xsd now how do I read in the xml?

What is the best practice method of reading an xml file to instance a collection of a .cs object generated via xsd.exe? 读取xml文件以实例化通过xsd.exe生成的.cs对象的集合的最佳实践方法是什么?

I used xsd.exe to generate an .xsd file (schema) and then to generate a .cs file. 我使用xsd.exe生成.xsd文件(模式),然后生成.cs文件。 I followed the steps here: http://ctrlf5.net/?p=235 and it worked great. 我按照这里的步骤进行操作: http : //ctrlf5.net/?p=235 ,效果很好。

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. 我不想使用XmlReader并编写一堆代码来浏览文档树,并沿途设置所有公共设置器。 My xml document is long and painful. 我的xml文档冗长而痛苦。 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. 我刚刚使用过xsd的新手,它使我的.cs文件变得多么酷-但是如果我现在必须编写500行代码来实例化我的类,那真是太棒了。 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和代码的样子:

<?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<>: 在这里,我尝试反序列化为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<>: 如果我尝试反序列化不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[]'
var xs = new XmlSerializer(typeof(List<YourClassGeneratedByXsd>));
List<YourClassGeneratedByXsd> list;
using (var reader = XmlReader.Create(fileName))
{
    list = (List<YourClassGeneratedByXsd>)xs.Deserialize(reader);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM