vb.net xml xpath wildcards

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marfi95

    vb.net xml xpath wildcards

    I'm just starting to learn xpath, so if I have the following xml

    <Root>
    <Data 1>
    <Child 1></Child 1>
    <Child 2></Child 2>
    </Data 1>
    <Data 2>
    <Child 1></Child 1>
    <Child 2></Child 2>
    </Data 2>
    </Root>

    Is there an xpath expression with SelectNodes that I can use to get
    the number of nodes that start with <Data>

    I thought I could use "Root/Data*", but that didn't seem to work.

    Thanks !
    Mark

  • Stephany Young

    #2
    Re: vb.net xml xpath wildcards

    Dim count As Integer = 0

    Dim nodelist = As XmlNodeList = xmldocument.Sel ectNodes("//*")

    For Each node As XmlNode In nodelist
    If node.Name.Start sWith("Data") Then count += 1
    Next

    Console.WriteLi ne(count)


    "marfi95" <marfi95@yahoo. comwrote in message
    news:1171667992 .212599.99240@a 75g2000cwd.goog legroups.com...
    I'm just starting to learn xpath, so if I have the following xml
    >
    <Root>
    <Data 1>
    <Child 1></Child 1>
    <Child 2></Child 2>
    </Data 1>
    <Data 2>
    <Child 1></Child 1>
    <Child 2></Child 2>
    </Data 2>
    </Root>
    >
    Is there an xpath expression with SelectNodes that I can use to get
    the number of nodes that start with <Data>
    >
    I thought I could use "Root/Data*", but that didn't seem to work.
    >
    Thanks !
    Mark
    >

    Comment

    • Stephany Young

      #3
      Re: vb.net xml xpath wildcards

      However if you Xml was constructed thus:

      <Root>
      <Data id="1">
      <Child id="1" />
      <Child id="2" />
      </Data>
      <Data id="2">
      <Child id="1" />
      <Child id="2" />
      </Data>
      </Root>

      then you could simply use:

      Console.WriteLi ne(xmldocument. SelectNodes("//Data").Count)


      "Stephany Young" <noone@localhos twrote in message
      news:uGTLLJiUHH A.3500@TK2MSFTN GP05.phx.gbl...
      Dim count As Integer = 0
      >
      Dim nodelist = As XmlNodeList = xmldocument.Sel ectNodes("//*")
      >
      For Each node As XmlNode In nodelist
      If node.Name.Start sWith("Data") Then count += 1
      Next
      >
      Console.WriteLi ne(count)
      >
      >
      "marfi95" <marfi95@yahoo. comwrote in message
      news:1171667992 .212599.99240@a 75g2000cwd.goog legroups.com...
      >I'm just starting to learn xpath, so if I have the following xml
      >>
      ><Root>
      > <Data 1>
      > <Child 1></Child 1>
      > <Child 2></Child 2>
      > </Data 1>
      > <Data 2>
      > <Child 1></Child 1>
      > <Child 2></Child 2>
      > </Data 2>
      ></Root>
      >>
      >Is there an xpath expression with SelectNodes that I can use to get
      >the number of nodes that start with <Data>
      >>
      >I thought I could use "Root/Data*", but that didn't seem to work.
      >>
      >Thanks !
      >Mark
      >>
      >

      Comment

      • marfi95

        #4
        Re: vb.net xml xpath wildcards

        On Feb 16, 5:33 pm, "Stephany Young" <noone@localhos twrote:
        However if you Xml was constructed thus:
        >
        <Root>
        <Data id="1">
        <Child id="1" />
        <Child id="2" />
        </Data>
        <Data id="2">
        <Child id="1" />
        <Child id="2" />
        </Data>
        </Root>
        >
        then you could simply use:
        >
        Console.WriteLi ne(xmldocument. SelectNodes("//Data").Count)
        >
        "Stephany Young" <noone@localhos twrote in message
        >
        news:uGTLLJiUHH A.3500@TK2MSFTN GP05.phx.gbl...
        >
        >
        >
        Dim count As Integer = 0
        >
        Dim nodelist = As XmlNodeList = xmldocument.Sel ectNodes("//*")
        >
        For Each node As XmlNode In nodelist
        If node.Name.Start sWith("Data") Then count += 1
        Next
        >
        Console.WriteLi ne(count)
        >
        "marfi95" <marf...@yahoo. comwrote in message
        news:1171667992 .212599.99240@a 75g2000cwd.goog legroups.com...
        I'm just starting to learn xpath, so if I have the following xml
        >
        <Root>
        <Data 1>
        <Child 1></Child 1>
        <Child 2></Child 2>
        </Data 1>
        <Data 2>
        <Child 1></Child 1>
        <Child 2></Child 2>
        </Data 2>
        </Root>
        >
        Is there an xpath expression with SelectNodes that I can use to get
        the number of nodes that start with <Data>
        >
        I thought I could use "Root/Data*", but that didn't seem to work.
        >
        Thanks !
        Mark- Hide quoted text -
        >
        - Show quoted text -
        Thanks ! I'll try that. I have some control over the XML itself, so
        I might go the way you suggested in the 2nd approach.

        Comment

        Working...