Visual Basic,读取XML多级节点和公共属性

时间:2016-01-09 23:34:55

标签: xml vb.net nodes elements

我有一个每月更改的XML数据文件。

它包含来自高端打印系统打印的作业的数据。我需要读取每个页面的覆盖率数据,我可以检索作业名称和大部分数据,但我似乎无法弄清楚如何准备每个页面的单独覆盖(将其链接到特定页面)? / p>

示例文件:

<JOBS01_2016> 
  <JOB_01_06_2016_16_48_17> 
    <JOB> 
      <TICKETID>101</TICKETID>  
      <JOBNAME>A4 booklet and creep test file.pdf</JOBNAME>  
      <ERRORMESSAGE>Job processing was cancelled</ERRORMESSAGE> 
    </JOB> 
  </JOB_01_06_2016_16_48_17>  
  <JOB_01_09_2016_20_48_39> 
    <JOB> 
      <TICKETID>103</TICKETID>  
      <JOBNAME>BlackA45percol.pdf</JOBNAME>  
      <HEIGHT>6916</HEIGHT>  
      <WIDTH>9721</WIDTH> 
    </JOB>  
    <MEDIA> 
      <MEDIAHEIGHT>7016</MEDIAHEIGHT>  
      <MEDIAWIDTH>9921</MEDIAWIDTH>  
      <MEDIANAME>Intec Light</MEDIANAME>  
      <NUMBEROFPAGES>4</NUMBEROFPAGES> 
    </MEDIA>  
    <QUALITY> 
      <RESOLUTIONX>1200</RESOLUTIONX>  
      <RESOLUTIONY>600</RESOLUTIONY>  
      <COLORMODE>4</COLORMODE>  
      <COLORMODENAME>CMYK</COLORMODENAME> 
    </QUALITY>  
    <COVERAGE> 
      <PAGE> 
        <PAGENUMBER>1</PAGENUMBER>  
        <C>3483675</C>  
        <M>3483675</M>  
        <Y>3948165</Y>  
        <K>3483675</K> 
      </PAGE>  
      <PAGE> 
        <PAGENUMBER>2</PAGENUMBER>  
        <C>0</C>  
        <M>3483675</M>  
        <Y>3948165</Y>  
        <K>3483675</K> 
      </PAGE>  
      <PAGE> 
        <PAGENUMBER>3</PAGENUMBER>  
        <C>3483675</C>  
        <M>3483675</M>  
        <Y>464490</Y>  
        <K>3483675</K> 
      </PAGE>  
      <PAGE> 
        <PAGENUMBER>4</PAGENUMBER>  
        <C>3483675</C>  
        <M>3483675</M>  
        <Y>3948165</Y>  
        <K>0</K> 
      </PAGE> 
    </COVERAGE> 
  </JOB_01_09_2016_20_48_39> 
</JOBS01_2016>

我编写了代码来引入作业名称(父节点)?

     Dim xml As New XmlDocument
    xml.Load("Jobs_01_2016.xml")

    ListBox1().Items.Clear()

    For Each node As XmlNode In xml.DocumentElement.SelectNodes("*")
        ListBox1.Items.Add(node.Name)
    Next

然后用户选择作业名称,并获得作业名称,作业高度,宽度,分辨率和页数,以及每页的覆盖范围(链接到页码)。 这个我似乎无法得到? 我无法弄清楚如何结合所选作业和特定页码来阅读它?

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) _
    Handles ListBox1.SelectedIndexChanged
    Dim Fred As String
    Dim sbReadXML As New StringBuilder

    Fred = ListBox1.SelectedItem.ToString()
    Dim xml As New XmlDocument
    xml.Load("Jobs_01_2016.xml")

    Dim Jobname As String

    Jobname = xml.SelectSingleNode("/JOBS01_2016/" + Fred + "/JOB/JOBNAME").InnerText
    sbReadXML.Append("Job Name: ")
    sbReadXML.Append(Jobname)
    sbReadXML.AppendLine()

    Dim MediaHeight As Integer = Integer.Parse(xml.SelectSingleNode("/JOBS01_2016/" + Fred + "/MEDIA/MEDIAHEIGHT").InnerText)
    sbReadXML.Append("Media Height: ")
    sbReadXML.Append(MediaHeight)
    sbReadXML.AppendLine()
    Dim MediaWidth As Integer = Integer.Parse(xml.SelectSingleNode("/JOBS01_2016/" + Fred + "/MEDIA/MEDIAWIDTH").InnerText)
    sbReadXML.Append("Media Width: ")
    sbReadXML.Append(MediaWidth)
    sbReadXML.AppendLine()
    Dim Nopages As Integer = Integer.Parse(xml.SelectSingleNode("/JOBS01_2016/" + Fred + "/MEDIA/NUMBEROFPAGES").InnerText)
    sbReadXML.Append("No of Pages: ")
    sbReadXML.Append(Nopages)
    sbReadXML.AppendLine()
    Dim Printtime As String
    Printtime = xml.SelectSingleNode("/JOBS01_2016/" + Fred + "/PRINT/PRINTTIME").InnerText
    sbReadXML.Append("Print Time: ")
    sbReadXML.Append(Printtime)
    sbReadXML.AppendLine()
    Dim Resolutionx As Integer = Integer.Parse(xml.SelectSingleNode("/JOBS01_2016/" + Fred + "/QUALITY/RESOLUTIONX").InnerText)
    sbReadXML.Append("Horizontal Resolution: ")
    sbReadXML.Append(Resolutionx)
    sbReadXML.AppendLine()
    Dim Resolutiony As Integer = Integer.Parse(xml.SelectSingleNode("/JOBS01_2016/" + Fred + "/QUALITY/RESOLUTIONY").InnerText)
    sbReadXML.Append("Vertical Resolution: ")
    sbReadXML.Append(Resolutiony)
    sbReadXML.AppendLine()

    sbReadXML.Append(": ")

    txtContent.Text = sbReadXML.ToString 'Show Contents
End Sub

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试xml linq

&#13;
&#13;
Imports System.Xml
Imports System.Xml.Linq
Module Module1

    Const FILENAME As String = "c:\temp\test.xml"

    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)

        Dim jobs = doc.Elements().Select(Function(s) New With {
                                             .name = s.Name.LocalName,
                                             .jobs = s.Elements().Select(Function(t) New With {
                                                                             .jobName = t.Name.LocalName,
                                                                             .job = New With {
                                                                                 .ticketID = CType(t.Element("JOB").Element("TICKETID"), Integer),
                                                                                 .jobname = CType(t.Element("JOB").Element("JOBNAME"), String),
                                                                                 .height = CType(t.Element("JOB").Element("HEIGHT"), String),
                                                                                 .width = CType(t.Element("JOB").Element("WIDTH"), String),
                                                                                 .errorMsg = CType(t.Element("JOB").Element("ERRORMESSAGE"), String)
                                                                             },
                                                                            .media = t.Elements().Where(Function(u) u.Name.LocalName = "MEDIA").Select(Function(v) New With {
                                                                                 .height = CType(v.Element("MEDIAHEIGHT"), Integer),
                                                                                 .width = CType(v.Element("MEDIAWIDTH"), Integer),
                                                                                 .name = CType(v.Element("MEDIANAME"), String),
                                                                                 .pages = CType(v.Element("NUMBEROFPAGES"), Integer)
                                                                             }).FirstOrDefault(),
                                                                            .quality = t.Elements().Where(Function(u) u.Name.LocalName = "QUALITY").Select(Function(v) New With {
                                                                                 .x = CType(v.Element("RESOLUTIONX"), Integer),
                                                                                 .y = CType(v.Element("RESOLUTIONY"), Integer),
                                                                                 .mode = CType(v.Element("COLORMODE"), Integer),
                                                                                 .name = CType(v.Element("COLORMODENAME"), String)
                                                                             }).FirstOrDefault(),
                                                                            .coverage = t.Elements().Where(Function(u) u.Name.LocalName = "COVERAGE").Select(Function(v) New With {
                                                                                 .pages = v.Elements("PAGE").Select(Function(w) New With {
                                                                                    .pageNumber = CType(w.Element("PAGENUMBER"), Integer),
                                                                                    .c = CType(w.Element("C"), Integer),
                                                                                    .m = CType(w.Element("M"), Integer),
                                                                                    .y = CType(w.Element("Y"), Integer),
                                                                                    .k = CType(w.Element("K"), Integer)
                                                                                }).ToList()
                                                                             }).FirstOrDefault()
                                                                        }).ToList()
                                         }).FirstOrDefault()

    End Sub

End Module
​
&#13;
&#13;
&#13;