1

When I load this XML node, the HTML within the node is being completely stripped out.

This is the code I use to get the value within the node, which is text combined with HTML:

var stuff = innerXml.Descendants("root").Elements("details").FirstOrDefault().Value;

Inside the "details" node is text that looks like this:

"This is <strong>test copy</strong>. This is <a href="#">A Link</a>"

When I look in "stuff" var I see this:

"This is test copy. This is A Link". There is no HTML in the output... it is pulled out.

Maybe Value should be innerXml or innerHtml? Does FirstOrDefault() have anything to do with this?

I don't think the xml needs a "cdata" block...

HEre is a more complete code snippet:

 announcements =
                from link in xdoc.Descendants(textContainer).Elements(textElement)
                where link.Parent.Attribute("id").Value == Announcement.NodeId
                select new AnnouncmentXml
                {
                    NodeId = link.Attribute("id").Value,
                    InnerXml = link.Value
                };

XDocument innerXml;
innerXml = XDocument.Parse(item.InnerXml);
var abstract = innerXml.Descendants("root").Elements("abstract").FirstOrDefault().Value;

Finally, here is a snippet of the Xml Node. Notice how there is "InnerXml" within the standard xml structure. It starts with . I call this the "InnerXml" and this is what I am passing into the XDocument called InnerXml:

 <text id="T_403080"><root> <title>How do I do stuff?</title> <details> Look Here <a href="http://" target=" _blank">Some Form</a>. Please note that lorem ipsum dlor sit amet.</details> </root></text> 

[UPDATE]

I tried to use this helper lamda, and it will return the HTML but it is escaped, so when it displays on the page I see the actual HTML in the view (it shows instead of giving a link, the tag is printed to screen:

Title = innerXml.Descendants("root").Elements("title").FirstOrDefault().Nodes().Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString());

So I tried both HTMLEncode and HTMLDecode but neither helped. One showed the escaped chars on the screen and the other did nothing:

Title = 
                        System.Web.HttpContext.Current.Server.HtmlDecode(
                            innerXml.Descendants("root").Elements("details").Nodes().Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString())
                        );
1
  • The Value property gives you the string value, not any markup. Your Aggregate approach looks right if you want the markup of any child nodes as a string. If using that does not give you the result you want in your ASP.NET context then you need to explain in more detail how you use it. So what are you doing with the Title you build with Nodes().Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.ToString()), sb => sb.ToString())? Commented May 11, 2011 at 12:20

2 Answers 2

2

I ended up using an XmlDocument instead of an XDocument. It doesn't seem like LINQ to XML is mature enough to support what I am trying to do. THere is no InnerXml property of an XDoc, only Value.

Maybe someday I will be able to revert to LINQ. For now, I just had to get this off my plate. Here is my solution:

// XmlDoc to hold custom Xml within each node
        XmlDocument innerXml = new XmlDocument();
        try
        {
            // Parse inner xml of each item and create objects
            foreach (var faq in faqs)
            {
                innerXml.LoadXml(faq.InnerXml);

                FAQ oFaq = new FAQ();

                #region Fields
                // Get Title value if node exists and is not null
                if (innerXml.SelectSingleNode("root/title") != null)
                {
                    oFaq.Title = innerXml.SelectSingleNode("root/title").InnerXml;
                }

                // Get Details value if node exists and is not null
                if (innerXml.SelectSingleNode("root/details") != null)
                {
                    oFaq.Description = innerXml.SelectSingleNode("root/details").InnerXml;
                }
                #endregion

                result.Add(oFaq);
            }
        }
        catch (Exception ex)
        {
            // Handle Exception
        } 
Sign up to request clarification or add additional context in comments.

Comments

1

I do think wrapping your details node in a cdata block is the right decision. CData basically indicates that the information contained within it should be treated as text, and not parsed for XML special characters. The html charaters in the details node, especially the < and > are in direct conflict with the XML spec, and should really be marked as text.

You might be able to hack around this by grabbing the innerXml, but if you have control over the document content, cdata is the correct decision.

In case you need an example of how that should look, here's a modified version of the detail node:

<details>
    <![CDATA[
         This is <strong>test copy</strong>. This is <a href="#">A Link</a>
    ]]>
</details>

3 Comments

Thank you for your response Evan. I don't have access to this XML, that would be nice - then I would add a CDATA block. This was apparently working with an older XPATH version without the cdata, so I should be able to get it to display the HTML. My Problem now is that I don't have access to the InnerXML property. When I try innerXml.Descendants("root").Elements("abstract").FirstOrDefault().InnerXml it doesn't work (obviously, InnerXml is not a property of an XElement). Value is the only accessor I see in intellisense. Any other ideas? : /
This 10 years old post helped me a great deal, thanks!
So glad to hear that. Ironically, I've long forgotten about cdata and xml in general now. :-).

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.