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>
2

1 Answer 1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.