[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Find a Node by its Value

Hi Friends,

I have a XML file like

<NewDataSet>
  <Products>
    <ProductID>1</ProductID>
    <ProductName>Chai</ProductName>
    <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
    <Deleted>0</Deleted>
  </Products>
  <Products>
    <ProductID>2</ProductID>
    <ProductName>Chang</ProductName>
    <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
    <Deleted>0</Deleted>
  </Products>
</NewDataSet>


I want to Find a <Products> Block its <Product> Nodes  Value is 1

Is there any direct way to find it.


Please help me.

Newmancroos
0
newmancroos_s
Asked:
newmancroos_s
  • 2
2 Solutions
 
akumanovaCommented:
using XPath query:

/NewDataSet/Products[ProductID="1"]

For more information about XPath, please visit
http://www.w3schools.com/xpath/default.asp
0
 
trainsdseCommented:
What language are you wanting this in so we can show you how to implement the XPath query?
0
 
kcchenkdCommented:

  Dim odoc As New System.Xml.XmlDocument
        Dim sProductXML As String
        odoc.Load("filename.xml")
        Dim oNode As Xml.XmlNode
        For Each oNode In odoc.SelectNodes("/NewDataSet/Products")
            If oNode.SelectSingleNode("ProductId").InnerText = "1" Then
                sProductXML = oNode.OuterXml
                Exit For
            End If
        Next

SO sProductXML is block of xml which productID IS 1

hope it helps
0
 
trainsdseCommented:
Judging from you document, you are getting this from a .net dataset. You could just go straight to it by:

Dim odoc As New System.Xml.XmlDocument
odoc.LoadXml(dataSetName.GetXml())
Dim oNode as Xml.XmlNode
oNode=odoc.SelectSingleNode("/NewDataSet/Products [ProductID='1']")

oNode would then be the node

<Products>
    <ProductID>1</ProductID>
    <ProductName>Chai</ProductName>
    <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
    <Deleted>0</Deleted>
  </Products>
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now