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)
https://intecprinters.iweb-storage.com/s/IjU2OTNlNjVkNmExODkyMDlmNjU5N2MyNiI.jaEay6nxR-v-gaiJwcVNiP113QI
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
ListBox1().Items.Clear()
' 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)
Next
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)
ListBox2().Items.Clear()
xmldoc.Load(fs)
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
ListBox2.Items.Add(str)
Next
'***********************************************************************************************
'
' Display Job details about the job in comments window (Text Box)
xmldoc.Load("Jobs_01_2016.xml")
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)
sbReadxml.AppendLine()
'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: ")
sbReadxml.Append(Printtime)
sbReadxml.AppendLine()
'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: ")
sbReadxml.Append(MediaHeight)
sbReadxml.AppendLine()
'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: ")
sbReadxml.Append(MediaWidth)
sbReadxml.AppendLine()
'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: ")
sbReadxml.Append(Nopages)
sbReadxml.AppendLine()
'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: ")
sbReadxml.Append(Resolutionx)
sbReadxml.AppendLine()
'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: ")
sbReadxml.Append(Resolutiony)
sbReadxml.AppendLine()
'
'
'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?