1

I have a basic xml file that looks like this.

    <root>
     <item>
        <title><p>some title</p></title>
     </item>
    ...
    </root>

What I want, is to get the whole title string including the html tag of the xml using linq and displaying it in a repeater . I can get the title with no problem, but the <p> tag is being stripped out.

If I use
title = item.Element("title").ToString(), it works somehow but I get all the xml tag as well - meaning the title is not displayed in html.

I already tried with encoding the "<" with "&lt;" but to do this makes the xml hard to read.

What would be a possible solution besides using CDATA and encoding?

Cheers Terry

1
  • would you consider manually parsing the XML? Commented Aug 7, 2009 at 9:47

3 Answers 3

1

Create a reader from the title element and read InnerXml:

    static void Main(string[] args)
    {
        string xml = "<root><item><title><p>some title</p></title></item></root>";

        XDocument xdoc = XDocument.Parse(xml);
        XElement te = xdoc.Descendants("title").First();
        using (XmlReader reader = te.CreateReader())
        {
            if (reader.Read())
                title = reader.ReadInnerXml();
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks bruno for the suggestion, unfortunately it didn't work. Could you give me an example of how I would combine this with the linq select statement I'm using? - Terry
thanks bruno, you pointed me in the right direction. Will take a little time to adapt it to my needs, but now I know where to start.
0

See Best way to get InnerXml of an XElement? for some ideas on how to get the "InnerXml" of an XElement.

Comments

0

XElement x = XElement.Parse(your xml);

var y= x.Descendants("title").Descendants();

Then iterate y for a list of the contents of the title elements.

BTW, LINQPad (http://www.linqpad.net) is a handy free tool for trying out LINQ-XML.

2 Comments

Hi ebpower, I tried that but somehow couldn't get it to work. Thanks for the hint with LINQPad, I had totally forgotten about it, been a while since I worked with it.
Hmm - i pasted from my LINQPad window. I used "<root><item><title><p>some title</p></title></item></root>" in place of the "your xml" parameter to XElement.Parse.

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.