locked
XML text node? RRS feed

  • Question

  • I have an XML file that has one text node and several child nodes. Say $xml points to the node, how do I get just the text node value from the node? If I do something like $xml.Value I get a concatentation of all the child nodes and my text node. I am not sure how to pick out just the text node?

    Thank you.


    Kevin Burton

    Tuesday, September 10, 2013 5:28 PM

Answers

  • Kevin,

    The # confuses PowerShell, so you have to put quotes around the node name.

    $xml.EntitySet.Actions.Action[0].'#text'

    • Marked as answer by KevinBurton Wednesday, September 18, 2013 10:05 PM
    Wednesday, September 11, 2013 5:02 PM

All replies

  • could we see an example of the node you are working with?
    Tuesday, September 10, 2013 5:29 PM
  • The xml that I am working with is very long but it looks like:

    <Root>

    This is some root text.

       <A>

          This is some text in the A node.

       </A>

       <B>

          This is some text in the B node.

       </B>

    </Root>

    I want to get the string 'This is some root text.'.


    Kevin Burton

    Tuesday, September 10, 2013 5:39 PM
  • Kevin,

    Try $xml.Root.'#text'

    • Proposed as answer by Jordan Mills Tuesday, September 10, 2013 10:35 PM
    Tuesday, September 10, 2013 7:27 PM
  • Say all that is in $xml ([xml]$xml = get-content .\text.xml for example).

    $xml.Root.'#text'


    Edit: I see I'm late to this party.
    • Edited by Jordan Mills Tuesday, September 10, 2013 10:35 PM
    Tuesday, September 10, 2013 10:34 PM
  • Hi,

    I would like to confirm what is the current situation.

    Please feel free to let us know if you need further assistance.

    Regards.

    If you have any feedback on our support, please click here


    Vivian Wang
    TechNet Community Support

    Wednesday, September 11, 2013 10:28 AM
  • I tried the "#text" but it didn't work as i expected.

    First i read my XML file in. As expected there is a root node called <EntitySet> with an <Actions> sub node and one or more <Action> nodes. I picked the first one:

    $xml.EntitySet.Actions.Action[0]

    Name                                       Type                                       #text                                     Parameters                               
    ----                                       ----                                       -----                                     ----------                               
    CreateLike                                 PresentationDefaultRepresentation          ...                                       Parameters  

    As you can see from the output there seems to be a '#text' node but when I try to access its value I get:

    $xml.EntitySet.Actions.Action[0].#text
    Missing property name after reference operator.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingPropertyName

    How does this correspond to the XML calls for XmlElement? 

    Thanks again.


    Kevin Burton

    Wednesday, September 11, 2013 4:52 PM
  • Kevin,

    The # confuses PowerShell, so you have to put quotes around the node name.

    $xml.EntitySet.Actions.Action[0].'#text'

    • Marked as answer by KevinBurton Wednesday, September 18, 2013 10:05 PM
    Wednesday, September 11, 2013 5:02 PM