Innertext from XElement?

12,945

You're trying to get the exact opposite of the InnerText / Value properties: the raw XML content.

You can get the content including the outer node by calling element.ToString().

If you want to exclude the outer tag, you can call String.Concat(element.Nodes()).

Share:
12,945

Related videos on Youtube

Kevin
Author by

Kevin

For years I developed ILE RPGIV software for the IBM AS/400 iSeries midrange computer. I made the leap to .Net years ago and love it. I've worked in a variety of industries from manufacturing, financial, and defense. I'm constantly amazed that even to this day, I learn something new every single day.

Updated on June 04, 2022

Comments

  • Kevin
    Kevin almost 2 years

    I'm having a hard time getting the correct value from the innertext of an XElement. First, here's the XML that I'm using. This is a copy of our production data that results from a process in our workflow. In other words, I can't change the XML, I can only parse it. The element whose innertext I'd like to get has data inside that looks like XML, but it isn't. It is straight text from the tool that produced the XML. The element is called <creatorshapeutildata:

    Picture of XML data

    Here is the line of code I've tried:

    CreatorShapeUtilData = element.Descendants("creatorshapeutildata").Single().Value;
    

    I've also tried this:

    CreatorShapeUtilData = element.Descendants("creatorshapeutildata").First().Value;
    

    I've also tried this:

    CreatorShapeUtilData = element.Element("creatorshapeutildata").Value;
    

    Unfortunately, the value that gets returned in every case looks like this:

    33012-1true#FFFF003#FFFFFF2743337743358

    I need the value returned to look like this:

    "<creatorData type="object"><type type="int">33012</type>..."
    

    This piece I'm working on is part of a larger program that uses XDocument, XElement, etc. I know an XmlElement has an InnerText property, but I think XElement does not, since I can't seem to find it in Intellisense.

    So, is there any possible way to grab the exact text between the creatorshapeutil tags?