Example of Reading XML in VB using class generated by XSD

590 Views Asked by At

I have generated a Visual Basic class by using the XSD.exe utility in Visual Studio. I was wondering if anyone had an example of how you process the data in Visual Basic. The examples on MSDN are pretty poor. Pointing me to example code, even in Csharp, would be fine.

1

There are 1 best solutions below

0
On

Assuming you have a class MyClass that's been generated from your XSD, and you have a file with XML data that fits that class in MySample.xml, you'd do something like this (sorry, I'm not fluent in VB - this is 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!
}

Does that help??