XElement value in C#

10,567

Solution 1

You can do it slightly more simply than using Descendants - the Nodes method only returns the direct child nodes:

XElement element = XElement.Parse(
    @"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;

Note that this will work even in the case where the child elements came before the text node, like this:

XElement element = XElement.Parse(
    @"<someNode><child>1</child><child>2</child>some value</someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;

Solution 2

There is no direct way. You'll have to iterate and select. For instance:

var doc = XDocument.Parse(
    @"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var textNodes = from node in doc.DescendantNodes()
                where node is XText
                select (XText)node;
foreach (var textNode in textNodes)
{
    Console.WriteLine(textNode.Value);
}
Share:
10,567
shadeglare
Author by

shadeglare

private "whatever" developer

Updated on June 23, 2022

Comments

  • shadeglare
    shadeglare almost 2 years

    How to get a value of XElement without getting child elements?

    An example:

    <?xml version="1.0" ?>
    <someNode>
        someValue
        <child>1</child>
        <child>2</child>
    </someNode>
    

    If i use XElement.Value for <someNode> I get "somevalue<child>1</child><child>2<child>" string but I want to get only "somevalue" without "<child>1</child><child>2<child>" substring.

  • shadeglare
    shadeglare almost 14 years
    Great thanks. In my program I'm needed in full enumeration of xml nodes and changes every values. With your sample I'm not needed in a recursive enum of xml nodes.
  • Jon Skeet
    Jon Skeet almost 14 years
    @MaxFX: This will still recursively enumerate all nodes within the element though. It's hidden, but it's still happening. See my answer for a way of avoiding that.
  • Ramón García-Pérez
    Ramón García-Pérez about 11 years
    If I want to get the value of all the nodes do I need to necessarily iterate the list of nodes? I'm interested in the values of the nodes that are not of type XText
  • Jon Skeet
    Jon Skeet about 11 years
    @BBHorus: Well if you want to look at all the nodes, then yes, you need to iterate over them. It's really not clear what you're trying to do - you might want to add a new question.
  • Ramón García-Pérez
    Ramón García-Pérez about 11 years
    Oh never mind now I realized that I do need some of the text nodes but the first and last. I have this element: <package>package <name>com</name>.<name>sample</name>.<name>app1</name>;</pac‌​kage> What I want is a value like this: com.sample.app1, I think I need to use some regex or something else like iterate the child nodes and get only the elements skipping the first and last ones.
  • Jon Skeet
    Jon Skeet about 11 years
    @BBHorus: You should almost certainly not use a regular expression. It's almost always the wrong approach to XML handling.
  • Ramón García-Pérez
    Ramón García-Pérez about 11 years
    Not to the XML is to the value in the XML element, it always contains these two parts: starts with "package ", and ends with ";" those I don't want but the rest of it. Thank you for your clarifications and help
  • Porkopek
    Porkopek almost 8 years
    You can simply write var textNodes = doc.DescendantNodes().OfType<XText>();