The java namespace prefix is not defined, but it is used in the xsi:type attributes of your .xml document. If you add an attribute that defines it it to the root of your document, the error will go away.
<rootDB xmlns:java="http://tempuri3.org">
As we later found out, on top of this the xsi:type is messing up the generation of the classes. Badly.
Using XmlSerializer
<listOfTrack xsi:type="java:com.prorfactor.top100.database.Track">
The name of the element is listOfTrack
, in the default namespace. Its type however is com.prorfactor.top100.database.Track
in the namespace the java prefix is referring to. This happens because the xsi:type
attribute changes it. xsd.exe can deal with this, but it needs a little help.
The way I got it to work is to create two .xsd
s. One for the java namespace and the other for the namespace of the document. They import each other.
As an example I'll show the bit that describes the start of the document where the names and types hop around a lot.
<rootDB xmlns="http://tempuri2.org"
xmlns:java="http://tempuri3.org">
<listOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="java:com.prorfactor.top100.database.Category">
<listOfTrack xsi:type="java:com.prorfactor.top100.database.Track">
For the default namespace, which I called http://tempuri2.org
, the schema has to list the names of the elements, because they are in the default namespace:
<xs:element name="rootDB" >
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="listOfCategory" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="listOfCategory"
type="java:com.prorfactor.top100.database.Category">
</xs:element>
<xs:element name="listOfTrack"
type="java:com.prorfactor.top100.database.Track">
</xs:element>
The schema for the java namespace, which I called http://tempuri3.org
lists the complextypes that are assigned to the elements using xsi:type:
<xs:complexType name="com.prorfactor.top100.database.Category"
xmlns:orig="http://tempuri2.org">
<xs:sequence>
<xs:element ref="orig:listOfTrack" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="orig:categoryName"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="com.prorfactor.top100.database.Track"
xmlns:orig="http://tempuri2.org">
<xs:sequence>
<xs:element ref="orig:slowestLapRecord" />
<xs:element ref="orig:listOfLapRecord" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="orig:trackName"/>
</xs:sequence>
</xs:complexType>
These two .xsd
s I compiled and the generated classes manage to deserialize the .xml file.
IF I manually add the java namespace, plus specify the default namespace to the .xml file
So, to be honest, I think you're better off using LINQ to XML:
Using LINQ to XML
XDocument xElement = XDocument.Load(Server.MapPath(Url.Content("~/Content/xml/rFactorHotlapsData.xml")));
// just some selects to show how you select them
var cats = from cat in xElement.Descendants("listOfCategory")
select new
{
Name = cat.Descendants("categoryName").FirstOrDefault().Value,
Track = from track in cat.Descendants("listOfTrack")
select new
{
Name = track.Descendants("trackName").FirstOrDefault().Value,
LapRecord = from record in cat.Descendants("listOfLapRecord")
select new
{
driverName = record.Descendants("driverName").FirstOrDefault().Value,
sessionName = record.Descendants("sessionName").FirstOrDefault().Value,
date = record.Descendants("date").FirstOrDefault().Value
}
}
};
// And a demo of how to iterate over the result of the select
foreach (var category in cats)
{
Console.WriteLine(category.Name);
foreach (var track in category.Track)
{
Console.WriteLine(" " + track.Name);
foreach (var lapRecord in track.LapRecord)
{
Console.WriteLine(" " + lapRecord.driverName);
Console.WriteLine(" " + lapRecord.sessionName);
Console.WriteLine(" " + lapRecord.date);
}
}
}