locked
XElement - Xpath RRS feed

  • Question

  • User364803292 posted

    Hello

    How can I select a spesific attribute from an XElement object using XPath?

    Wednesday, April 9, 2014 3:02 AM

Answers

All replies

  • User697462465 posted

    Hi Taonias,

    There have a sample, maybe helps:

    Imports System
    Imports System.Xml
    Imports System.Xml.XPath
    
    Public Class MainClass
    
        Public Shared Sub Main()
            Dim root As XElement = _
                <Root>
                    <Child1>1</Child1>
                    <Child2>2</Child2>
                    <Child3>3</Child3>
                    <Child4>4</Child4>
                    <Child5>5</Child5>
                    <Child6>6</Child6>
                </Root>
            Dim el As XElement = root.XPathSelectElement("./Child4")
            Console.WriteLine(el)
    
        End Sub
    
    End Class

    Reference:

    http://www.java2s.com/Code/VB/XML-LINQ/SelectsanXElementusingaXPathexpression.htm

    Best Regards,
    Terry Guo

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Thursday, April 10, 2014 5:48 AM
  • User364803292 posted

    Thanks for replying

    This is my xml:

    <RootNode testAttr="my attr">
      <SubNode subAttr="My sub attr">
        My Sub Text
      </SubNode>
    </RootNode>

    suppose i need to select the value of the attribute testAttr what would be the xpath for that?

    Thursday, April 10, 2014 8:59 AM
  • User697462465 posted

    Hi Taonias,

    Please use the following XPath:

    RootNode/@testAttr

    More information about XPath please refer to:
    http://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

    Hope it helps.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 11, 2014 4:43 AM