Below is my code.
[XmlRootAttribute("book")]
public class BookHtml
{
[XmlElement("book-id")]
public string BookId { get; set; }
[XmlElement("book-xhtml")]
public BookHtmlMetadata BookXhtml { get; set; }
public String ToHtml()
{
return this.BookXhtml.Xhtml.ToString();
}
}
public class BookHtmlMetadata
{
[XmlElement("xhtml")]
public XElement Xhtml { get; set; }
}
public class Program
{
private static string GetXhtmlWithNoTags()
{
return "<content>" +
"<book>" +
"<book-id label=\"Book Id\">2</book-id>" +
"<book-xhtml label=\"Book Xhtml\">" +
"<xhtml>" +
"Copyright © 2010 . All rights reserved.<a href=\"/Home/Book.asp\">Best book ever</a>. " +
"</xhtml>" +
"</book-xhtml>" +
"</book>" +
"</content>";
}
static void Main(string[] args)
{
string xml = GetXhtmlWithNoTags();
XElement contentXml = XElement.Parse(xml);
var xmlSerializer = new XmlSerializer(typeof(BookHtml));
var list = new List<BookHtml>();
foreach (var child in contentXml.Elements())
{
list.Add((BookHtml)xmlSerializer.Deserialize(child.CreateReader()));
}
string contentToRender = list.Single().BookXhtml.Xhtml;
}
When I run this code I get an error on:
xmlSerializer.Deserialize(child.CreateReader());
The XmlReader must be on a node of type Element instead of a node of type Text.
How can I deserialize the content within <xhtml/> tags without losing any of the html tags such as <a href="/Home/Book.asp"> ? I should be able to use the xhtml and render the html tags/links in the browser.
Any ideas, suggessions greatly appreciated.