I have this XML file that I would like to parse into an object in C#:
- 001-0180914-5787994.xml (http://pastebin.com/bzzAnsQL)
So, I opened this xml file in VS Express 2013 for Desktop and clicked on the XML -> Generate Schema menu option. This generated two XSD schema files:
- 001-0180914-5787994.xsd (http://pastebin.com/QsvARtyB)
- 001-0180914-57879941.xsd (http://pastebin.com/FH4XNhvd)
I then wrote a batch script which uses the xsd.exe tool (comes with .NET SDK) to generate a C# class file from the schema like this:
@ECHO off
set xsdFile="C:\Users\Administrator\Desktop\test\001-0180914-5787994.xsd"
set outDirectory="C:\Users\Administrator\Desktop\test\out"
set xsdExeDir="C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools"
set language="CS"
cd %xsdExeDir%
xsd.exe "%xsdFile%" "C:\Users\Administrator\Desktop\test\001-0180914-57879941.xsd" /c /out:"%outDirectory%" /l:"%language%"
pause
When I ran the above batch script, it generated the following C# class:
- 001-0180914-57879941.cs (http://pastebin.com/wX8N0DAf)
Finally, I added this into a test console app project and I tried to generate an object out of my XML file and this auto-generated class like this:
class Program
{
static void Main(string[] args)
{
try
{
var order = Parse("001-0180914-5787994.xml");
Console.WriteLine("Success !!!");
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Console.WriteLine("Press any key to exit...");
Console.Read();
}
public static OrderResponseDetailComplete Parse(String XMLFile)
{
var settings = new XmlReaderSettings();
var obj = new OrderResponseDetailComplete();
var reader = XmlReader.Create(XMLFile, settings);
var serializer = new XmlSerializer(typeof(OrderResponseDetailComplete));
obj = (OrderResponseDetailComplete)serializer.Deserialize(reader);
reader.Close();
return obj;
}
}
When test program ran, I am getting this error:
What does this error mean? and how do I fix it?