2

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 &lt;a href="/Home/Book.asp"&gt; ? I should be able to use the xhtml and render the html tags/links in the browser.

Any ideas, suggessions greatly appreciated.

1 Answer 1

1

Since the interior of the xhtml tags could be a free form, you should change the type from XElement to string. Then, to escape the html/xhtml block from being processed you can use the CDATA to tell the deserialization routine that this is not quite valid xml, and you don't have a typed structure to use.

In Code:

public class BookHtmlMetadata
{
    [XmlElement("xhtml")]
    public string Xhtml { get; set; }
}

and

private static string GetXhtmlWithNoTags()
{
    return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><content>" +
                "<book>" +
                    "<book-id label=\"Book Id\">2</book-id>" +
                    "<book-xhtml label=\"Book Xhtml\">" +
                        "<xhtml><![CDATA[" +
                                "Copyright © 2010 . All rights reserved.<a href=\"/Home/Book.asp\">Best book ever</a>. " +
                        "!]]></xhtml>" +
                    "</book-xhtml>" +
                "</book>" +
            "</content>";
}

Now, if the xhtml block is valid xml, then you can use an XMLDocument to load the xml and traverse the tree.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.