使用XSD生成的类在VB中读取XML的示例

时间:2011-02-04 00:20:23

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

我在Visual Studio中使用XSD.exe实用程序生成了一个Visual Basic类。我想知道是否有人有一个如何在Visual Basic中处理数据的示例。 MSDN上的示例非常糟糕。将我指向示例代码,即使在Csharp中,也没关系。

1 个答案:

答案 0 :(得分:0)

假设你有一个从你的XSD生成的类MyClass,并且你有一个文件包含符合MySample.xml中该类的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!
}

这有帮助??