【问题标题】:Example of Reading XML in VB using class generated by XSDVB中使用XSD生成的类读取XML示例
【发布时间】:2011-06-21 01:37:48
【问题描述】:

我使用 Visual Studio 中的 XSD.exe 实用程序生成了一个 Visual Basic 类。我想知道是否有人有您如何在 Visual Basic 中处理数据的示例。 MSDN 上的例子很差。将我指向示例代码,即使在 Csharp 中,也可以。

【问题讨论】:

    标签: visual-studio-2008 xsd xsd.exe


    【解决方案1】:

    假设您有一个从 XSD 生成的类 MyClass,并且您有一个包含 XML 数据的文件,该文件适合 MySample.xml 中的该类,您会做这样的事情(抱歉,我不流利在 VB 中 - 这是 C#):

    // create the XML serializer class, based on your MyClass definition
    XmlSerializer ser = new XmlSerializer(typeof(MyClass));
    
    // create filestream to open & read the existing XML file
    FileStream fstm = new FileStream(@"C:\mysample.xml", FileMode.Open, FileAccess.Read);
    
    // call deserialize
    var result = ser.Deserialize(fstm);
    
    // if result is not null, and of the right type - use it!
    MyClass deserializedClass = (result as MyClass);
    if(deserializedClass != null)
    {
      // do whatever you want with your new class instance!
    }
    

    这有帮助吗??

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      相关资源
      最近更新 更多