0

When I do

XPathSelectElement("/root/title").ToString()

I get for example <title>this is an <strong>example</strong></title>. But I don't want to get <title> around the text.

When I do

XPathSelectElement("/root/title").Value

then it gets only the text without <strong></strong>

How can I solve this?

Ozkan
  • 2,011
  • 6
  • 29
  • 43

2 Answers2

0

From memory:

XPathSelectElement("/root/title/text()").ToString()

Alternatively, you might select all child nodes (/root/title/*) and join the their string representations

sehe
  • 374,641
  • 47
  • 450
  • 633
  • No. That gets the same as `.Value`. – R. Martinho Fernandes Apr 19 '11 at 09:34
  • I was still typing I guess :) – sehe Apr 19 '11 at 09:35
  • the first doesn't work, i get an error but dont know what exactly. and the second: '/root/title/*' gives a nullreferenceexception if there is only plain text in the xml node – Ozkan Apr 19 '11 at 10:34
  • You can, of course, check for nulls. However, I think the real trouble is that you wanted text nodes as well as elements. My xpath is (unfortunately) too sloppy to remember whether that was (standard) possible – sehe Apr 19 '11 at 10:39
0

You can create an XmlReader and read the inner xml from it.

XElement element = doc.XPathSelectElement("/root/title");
XmlReader reader = element.CreateReader();
reader.MoveToContent();
string innerXml = reader.ReadInnerXml();
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510