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

发布于 2024-12-13 10:43:37 字数 3813 浏览 2 评论 0原文

读取 xml 文件以实例化通过 xsd.exe 生成的 .cs 对象集合的最佳实践方法是什么?

我使用 xsd.exe 生成 .xsd 文件(架构),然后生成 .cs 文件。我按照这里的步骤操作: http://ctrlf5.net/?p=235 效果很好。

我不想使用 XmlReader 并编写一堆代码来导航文档树,并在此过程中设置所有公共设置器。我的 xml 文档又长又痛苦。我只想按下简单按钮并拥有我的收藏。作为一名新手,我刚刚使用 xsd 度过了一段美妙的时光 - 它使我的 .cs 文件变得多么酷 - 但如果我现在必须编写 500 行代码来实例化我的类 - 那就太棒了。只有现在有一种简单的方法来实例化我的收藏才有意义,但我只是不知道它是什么或如何用谷歌搜索它。发回这个问题已经回答很高兴接受。

这是我的 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>

这里我尝试反序列化为 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);

}

这是错误消息:

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[]'

如果我尝试反序列化而不是 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);

}

这会导致类似的错误:

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[]'

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[]'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2024-12-20 10:43:37
var xs = new XmlSerializer(typeof(List<YourClassGeneratedByXsd>));
List<YourClassGeneratedByXsd> list;
using (var reader = XmlReader.Create(fileName))
{
    list = (List<YourClassGeneratedByXsd>)xs.Deserialize(reader);
}
var xs = new XmlSerializer(typeof(List<YourClassGeneratedByXsd>));
List<YourClassGeneratedByXsd> list;
using (var reader = XmlReader.Create(fileName))
{
    list = (List<YourClassGeneratedByXsd>)xs.Deserialize(reader);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文