0

I have a function that returns the value from a specific tag in an XML document:

            XElement elem = XElement.Parse(xml_string);
            string ret = elem.Element(key).Value.ToString();
            return ret;

I'm trying to figure out how to create another method that returns the full string contents contained within a tag, including child tags and child values.

i.e. if I have:

<foo>
 Hello
 <child1>val1</child1>
 <child2>val2</child2>
</foo>

The method above properly returns 'Hello', but what I want is another method that returns:

Hello<child1>val1</child1><child2>val2</child2>
Yahia
  • 69,653
  • 9
  • 115
  • 144
joelc
  • 2,687
  • 5
  • 40
  • 60
  • Actually, that method returns "Hello val1val2" with some line breaks... – Jon Skeet Feb 02 '12 at 17:53
  • possible duplicate of [Best way to get InnerXml of an XElement?](http://stackoverflow.com/questions/3793/best-way-to-get-innerxml-of-an-xelement) – Cameron Feb 02 '12 at 17:53

1 Answers1

1

The easiest option is to spin through the collection returned by XElement.Nodes() and concatenate the XNode.ToString() values for all of those nodes. If you don't want it formatted (and it sounds like you don't), call XNode.ToString(SaveOptions.DisableFormatting)

MNGwinn
  • 2,394
  • 17
  • 18