Hi I need help

I have some XML files which have more children and grandchildren, than I am used to playing with. (Link to xml file below)

I need to be able to read the xml, determine the name Root node ,<JOBS_Jan_2016> and obtain the list of names of subsequent Parent nodes. Inside EACH parent node, I need to access the JOB child node, and JOB Name element node <JOB>.
As, an error could have occured while creating the data, I need to check how many Child Nodes are present, and if only 1, then skip (First 2 entries in XML files are such instances!)
IF 5 child nodes are present within the Job node, then it is likely all data was written correctly and I need to ensure a MEDIA CHILD node existsas one of them and must read the integer from this that relates to a number of pages that were printed.
I then need to sumarize from the Parent node selected: the Job name (from the JOB Child Node), the number of pages from the MEDIA child node. Get the COVERAGE data for each page under the COVERAGE child node, Which is a grandchild node.

The issue I face, is that I do not seem to be able to addredd what data in in the Coverage node, may be because these are grandchildren nodes?. (I can't for example select a specific job, and identify what coverage data may have been used in page 3 of that job)
When ever I select the path... it can't see any data for the specific coverage node.
I need to count how many entires there are, then for each page how many entries again (as the number of colors that make the page up can change). Having worked out how many colors there are I need to list the color name (which is the tag name) along with the number of pixels on that page.

But, I can't read any dat from a specific job /coverage node, or their inner values selectively.
Either I can read all values in one go, or none.

I have a simple basic form, with 1 button, 4 list boxes, and a Text box.
The code below, reads the Parent nodes, when clicking Button1, and populates listbox1 with Parent nodes. (the job names)
When you click on a job, it should display the list of child nodes in list box 2 and additional info in the txt box below.
List box 3, SHOULD get populated with a line for each page in the job, i.e. page 1, Page 2, Page 3.
And a list box 4 should populate WHEN a user clicks on the page number from listbox3. List box 4 should then show, the coverage from EACH color in the job. (But, I can't seem to access the page cooverage for each job, so can't populate the list boxes)

Imports System
Imports System.Xml
Imports System.IO
Imports System.Text
Imports System.Xml.XPath
Public Class Form1
    Private xdDoc As New XmlDocument()
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim xml As New XmlDocument
        xml.Load("Jobs_01_2016.xml") ' Open my Print JOB log 


        ' list all node names (XML file gets a node written for each print job arriving into the printing device)
        For Each node As XmlNode In xml.DocumentElement.SelectNodes("*")
            ListBox1.Items.Add(node.Name) ' display node names in Listbox 1  (Later clean up with REAL Print job name)

    End Sub
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim xmldoc As New XmlDocument()
        Dim xmlnode As XmlNodeList
        Dim JobSelected As String '  the name (Node) of the print job to be read
        Dim sbReadxml As New StringBuilder
        Dim JobsFile As XmlReader = New XmlTextReader("Jobs_01_2016.xml")
        Dim type = JobsFile.NodeType
        JobSelected = ListBox1.SelectedItem.ToString()
        Dim i As Integer
        Dim vsbReadXML As New StringBuilder
        Dim str As String
        Dim fs As New FileStream("Jobs_01_2016.xml", FileMode.Open, FileAccess.Read)
        xmlnode = xmldoc.GetElementsByTagName(JobSelected)

        '    MsgBox(xmlnode(i).ChildNodes.Count)
        For x = 0 To xmlnode(i).ChildNodes.Count - 1
            str = xmlnode(i).ChildNodes.Item(x).Name.ToString


        ' Display Job details about the job in comments window  (Text Box)
        xmlnode = xmldoc.GetElementsByTagName(JobSelected) ' select sepcific Job node from Listbox1

        'Check if XML nodes contain enough nodes.  (Less than 3 nodes, means job failed during output, so error message written into XML, and not all data nodes for completed jobs can be found)
        If xmlnode(0).ChildNodes.Count > 4 Then

            'Find Job Name and write it to Txt string
            sbReadxml.Append("Job Name: ")
            sbReadxml.Append(xmldoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/JOB/JOBNAME").InnerText)

            'Find Time Job was Printed and write it to Txt string
            Dim Printtime As String
            Printtime = xmldoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/PRINT/PRINTTIME").InnerText
            sbReadxml.Append("Print Time: ")

            'Find Paper Height and write it to Txt string
            Dim MediaHeight As Integer = Integer.Parse(xmldoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/MEDIA/MEDIAHEIGHT").InnerText)
            sbReadxml.Append("Media Height: ")

            'Find Paper Width and write it to Txt string
            Dim MediaWidth As Integer = Integer.Parse(xmldoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/MEDIA/MEDIAWIDTH").InnerText)
            sbReadxml.Append("Media Width: ")

            'Find Number of Pages in job being printed and write it to Txt string
            Dim Nopages As Integer = Integer.Parse(xmldoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/MEDIA/NUMBEROFPAGES").InnerText)
            sbReadxml.Append("No of Pages: ")

            'Find resolution (Horizontal) in job being printed and write it to Txt string
            Dim Resolutionx As Integer = Integer.Parse(xmldoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/QUALITY/RESOLUTIONX").InnerText)
            sbReadxml.Append("Horizontal Resolution: ")

            'Find resolution (Vertical) in job being printed and write it to Txt string
            Dim Resolutiony As Integer = Integer.Parse(xmldoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/QUALITY/RESOLUTIONY").InnerText)
            sbReadxml.Append("Vertical Resolution: ")
            'Create Loop, to read coverage for each page
            'But, careful, do not know which colors are in document, need to read color first, then pixel count 
        End If
        txtContent.Text = sbReadxml.ToString 'Show Contents

    End Sub
    Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
        'expand information on Job selected
        Dim xmlDoc As New XmlDocument ', xmlSelectedNode As XmlNode
        Dim JobSelected As String '  the name of the parent node of job to be read
        Dim Nodeselected As String '  the sub section in the jobfiles (Child Node) inside the job to be read
        Dim NoGrandChildNodes As Integer ' the number of subsequent nodes inside the Childnode
        JobSelected = ListBox1.SelectedItem.ToString()
        Nodeselected = ListBox2.SelectedItem.ToString()

        xmlDoc.Load("Jobs_01_2016.xml") 'Load the Xml file once if constantly in use.

        ' select child node of Selected JOB (Job Info/ Media Info / Coverage Info) from List box 2 list of preset nodes.
        'Identify how many subsequent nodes there are.
        NoGrandChildNodes = xmlDoc.GetElementsByTagName("/JOBS01_2016/" + JobSelected + "/" + NodeSelected).Count()

        MsgBox(NoGrandChildNodes) ' the above line does not count how many nodes.  (In particular IF the Node selected is coverage which has subsequent nodes)

        If NodeSelected = "MEDIA" Then
            Dim Nopages As Integer = Integer.Parse(xmlDoc.SelectSingleNode("/JOBS01_2016/" + JobSelected + "/MEDIA/NUMBEROFPAGES").InnerText) 'Media node selected from Parent node
            ' MsgBox(Nopages)
        End If

    End Sub

Please let me know what I ahve done wrong?

Re: Visual Basic 2013, XML can't read specific grandchildren nodes please help. 80 80

Hi squashspark, and welcome to DaniWeb.

To get at the data inside your Coverage node you could try selecting it with an XPath query, then selecting the nodes you want in the context of the current node. For example, in the code below line 4 selects a PAGE node, then line 7 uses the XPath query "./PAGENUMBER" to select a child node. Does this help?

    Dim xmlDoc As New XmlDocument
    Dim nodeList As XmlNodeList
    nodeList = xmlDoc.SelectNodes("/JOBS01_2016/JOB_01_09_2016_20_50_13/COVERAGE/PAGE")
    For Each pageNode As XmlNode In nodeList

BTW, it's generally a good thing to keep code examples as short as possible. You'll find it helps to narrow down issues, and you're also more likely to get a quicker response.

Be a part of the DaniWeb community

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