Xsd or class structure for the following xml (xhtml actually)
-
Monday, September 24, 2012 1:43 PM
Hi,
I have some table of content that is represented as xhtml as shown below (I am only pasting the interesting part) :
<ol> <li> <a href="wasteland-content.xhtml#ch1">I. THE BURIAL OF THE DEAD</a> <ol> <li> <a href="wasteland-content.xhtml#ch2">I.1 FOOO BAR</a> <ol> <li> <a href="wasteland-content.xhtml#ch2">I.1.1 FOOO BAR</a> </li> <li> <a href="wasteland-content.xhtml#ch2">I.1.2 FOOO BAR</a> </li> </ol> </li> <li> <a href="wasteland-content.xhtml#ch2">I.2 FOOO BAR</a> </li> </ol> </li> <li> <a href="wasteland-content.xhtml#ch2">II. A GAME OF CHESS</a> </li> <li> <a href="wasteland-content.xhtml#ch3">III. THE FIRE SERMON</a> </li> <li> <a href="wasteland-content.xhtml#ch4">IV. DEATH BY WATER</a> </li> <li> <a href="wasteland-content.xhtml#ch5">V. WHAT THE THUNDER SAID</a> </li> <li> <a href="wasteland-content.xhtml#rearnotes">NOTES ON "THE WASTE LAND"</a> </li> </ol><ol> is the root node, <li><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) :
// Node.cs [XmlRoot("ol")] [XmlInclude(typeof(li))] [XmlInclude(typeof(a))] public class Node { }
// ol.cs public class ol : Node { [XmlElement("li", typeof(li))] [XmlElement("a", typeof(a))] public List<Node> Nodes { get; set; } }
// li.cs public class li : Node { [XmlElement("a")] public a link { get; set; } public li() { } public li(a link) { this.link = link; } }
// a.cs public class a { [XmlAttribute("href")] public string Tref { get; set; } [XmlText] public string Text { get; set; } }namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string file = @"C:\Developer\nav.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(); } } }
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 ?
All Replies
-
Tuesday, September 25, 2012 2:12 AMWhat's the exception is?
Go go Doraemon!
-
Thursday, September 27, 2012 2:09 AMModerator
Hi Vincent Rich,
I've tested the code you post, without any modification, everything works well, no exception was thrown. Could you please clarify what's the exception you received?
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us

