Xsd or class structure for the following xml (xhtml actually)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have some table of content that is represented as xhtml as shown below (I am only pasting the interesting part) :
<pre class="prettyprint <ol>

wasteland-content.xhtml#ch1 I. THE BURIAL OF THE DEAD
<ol>

wasteland-content.xhtml#ch2 I.1 FOOO BAR
<ol>

wasteland-content.xhtml#ch2 I.1.1 FOOO BAR


wasteland-content.xhtml#ch2 I.1.2 FOOO BAR

</ol>


wasteland-content.xhtml#ch2 I.2 FOOO BAR

</ol>


wasteland-content.xhtml#ch2 II. A GAME OF CHESS


wasteland-content.xhtml#ch3 III. THE FIRE SERMON


wasteland-content.xhtml#ch4 IV. DEATH BY WATER


wasteland-content.xhtml#ch5 V. WHAT THE THUNDER SAID


wasteland-content.xhtml#rearnotes NOTES ON "THE WASTE LAND"

</ol>[/code]
<ol> is the root node, <a> is an entry in the current node and <ol> starts a child node.
So now I would like to be able to deserialize this xml into a data structure so I am starting with the 4 following classes (ol, li, a and Node.cs) :
<pre class="prettyprint // Node.cs
[XmlRoot("ol")]
[XmlInclude(typeof(li))]
[XmlInclude(typeof(a))]
public class Node
{
}[/code]
<br/>

<pre class="prettyprint // ol.cs
public class ol : Node
{
[XmlElement("li", typeof(li))]
[XmlElement("a", typeof(a))]
public List<Node> Nodes { get; set; }
}[/code]
<br/>

<pre class="prettyprint // li.cs
public class li : Node
{
[XmlElement("a")]
public a link { get; set; }

public li() { }

public li(a link)
{
this.link = link;
}
}[/code]
<br/>

<pre class="prettyprint // a.cs
public class a
{
[XmlAttribute("href")]
public string Tref { get; set; }
[XmlText]
public string Text { get; set; }
}[/code]

<pre class="prettyprint namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string file = @"C:Developernav.xml";
XmlSerializer xml = new XmlSerializer(typeof(Node));
FileStream ifs = new FileStream(file, FileMode.Open, FileAccess.Read);
Node result = (Node)xml.Deserialize(ifs);
ifs.Close();

Console.ReadKey();
}
}
}[/code]
<br/>


But I messed up in the definitions and when I try to load the xml given as example I get an exception.
What am I doing wrong ?

View the full article
 
Back
Top