Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:02:48+00:00 2026-05-29T16:02:48+00:00

Below is my code. [XmlRootAttribute(book)] public class BookHtml { [XmlElement(book-id)] public string BookId {

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:02:49+00:00Added an answer on May 29, 2026 at 4:02 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below code in Java throws Null pointer exception. public class New{ int i;
I am using the following code to get an xml string. public static string
I'm writing code to do Xml serialization. With below function. public static string SerializeToXml(object
Below code works fine: <?php session_start(); $_SESSION['color'] = 'blue'; class utilities { public static
Below is code used to search a string where Identity= exists and stores that
The below code is in PHP echo <input type='submit' name='delete' value='$t2[0]' class='delete_button' onclick='return confirm('Please
The below code is my Method logic . pulic String getData() { if (leg.length
The below code makes it easy to pass in a set HtmlParserOptions and then
in below code when i run it i get y=-124 and z=4294967172 can you
The below code is working fine. But if i change the html string in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.