hi
I need some help.
I have XML file with the folowing structure, and I wanted to know if there is a way to
get some FLD nodes from a LIN Node in 1 XPATH command so I can get these values in 1 command.
I need just the first FLD and the third FLD. and I want to get the XPathNodeIterator to iterate 3 times.
since there are 3 lines.
what is the way to do?

<TAB DIM="200" ID="POH2_1" SIZE="3" >
        <LIN NUM="1" >
            <FLD NAME="ITMREF" TYPE="Char" >ABC</FLD>
            <FLD NAME="LINAMT" TYPE="Decimal" >200</FLD>
            <FLD NAME="QTYUOM" TYPE="Decimal" >10</FLD>
            <FLD NAME="PUU" TYPE="Char" >EA</FLD>         
        </LIN>
        <LIN NUM="2" >           
            <FLD NAME="ITMREF" TYPE="Char" >DEF</FLD>
            <FLD NAME="LINAMT" TYPE="Decimal" >300</FLD>
            <FLD NAME="QTYUOM" TYPE="Decimal" >30</FLD>
            <FLD NAME="PUU" TYPE="Char" >EA</FLD>
        </LIN>
        <LIN NUM="3" >
            <FLD NAME="ITMREF" TYPE="Char" >XYZ</FLD>
            <FLD NAME="LINAMT" TYPE="Decimal" >500</FLD>
            <FLD NAME="QTYUOM" TYPE="Decimal" >40</FLD>
            <FLD NAME="PUU" TYPE="Char" >EA</FLD>
        </LIN>
</TAB>

thanks
Barak

Dani AI

Generated

Short answer for (and in response to ’s language question): select the LIN elements so the XPathNodeIterator iterates once per line, then use a relative XPath inside each LIN to pull the first and third FLD. Selecting "/TAB/LIN" produces an iterator that iterates 3 times. Selecting "/TAB/LIN/FLD[position()=1 or position()=3]" returns six nodes (two per LIN) — not one iteration per LIN.

Example (VB.NET using XPathNavigator):

Dim doc As New XmlDocument()
doc.Load("path-to-file.xml")
Dim nav As XPathNavigator = doc.CreateNavigator()
Dim linIt As XPathNodeIterator = nav.Select("/TAB/LIN")

While linIt.MoveNext()
    Dim linNav As XPathNavigator = linIt.Current

    ' Option A: two single-node queries
    Dim firstFld As XPathNavigator = linNav.SelectSingleNode("FLD[1]")
    Dim thirdFld As XPathNavigator = linNav.SelectSingleNode("FLD[3]")

    Dim v1 As String = If(firstFld IsNot Nothing, firstFld.Value, "")
    Dim v3 As String = If(thirdFld IsNot Nothing, thirdFld.Value, "")

    ' process v1 and v3
End While

A compact alternative inside the loop is linNav.Select("FLD[position()=1 or position()=3]"), which returns the two child nodes in document order.

Notes and cautions: XPath indexes are 1-based. Using position() relies on element order — if a field order can change, prefer attribute selection (for example FLD[@NAME='ITMREF'] and FLD[@NAME='QTYUOM']). Always null-check SelectSingleNode results before using .Value. For heavy repeated queries, compile the XPathExpression or use LINQ to XML for clearer projection.

Recommended Answers

All 2 Replies

what program language should be used
example xslt java ruby

hi, thanks for answering
I use VB.net

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.