LINQ to XML实现进度条 [英] LINQ to XML implement a progress bar

查看:88
本文介绍了LINQ to XML实现进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下函数(请参见下文中的项目代码),该函数遍历xml文件并提取数据并将数据写入文件.我要完成的工作是在写入文件时有一个进度条.我知道我从大小为0的文件开始,并且我最终以某个大小的文件结束:

I got the following function (see below for project code) that goes through an xml file and extracts data and writes the data to a file. What I want to accomplish is to have a progress bar as the file gets written. I know I start with a file of size 0 and I know I end up with a file of a certain size:

Dim info2 As New FileInfo("C:\Temp\test.txt")
Dim length2 As Long = info2.Length / 1024 'KB
ProgressBar1.Maximum = length2




但是,我在将休闲代码实现到我的项目中时遇到问题,感谢您的帮助:




However, I am having problems implementing the fallowing code into my project, I appreciate your help:

Dim intValue As Integer
        ProgressBar1.Maximum = lenth2

        Do While Not intValue = lenth2
            ProgressBar1.Value = intValue
            intValue = intValue + 1
        Loop



项目代码:



Project code:

strFilename = browsetofile()

If IsNothing(strFilename) Then

    MsgBox("No XML file was selected", vbCritical, "No XML file was selected")

    Exit Sub
End If


Dim xd As XDocument = XDocument.Load(strFilename)

Dim FILE_NAME As String = "C:\Temp\test.txt"

Dim objWriter As New System.IO.StreamWriter(FILE_NAME)


Dim strPipe As String = Chr(124)

Dim strSpace As String = Chr(10)


Dim strFinalString As String = ""
prgreebar()

Dim Records = (From datafields In xd.Descendants("record")
Select datafields)

'Me.Cursor = Cursors.WaitCursor



For Each ser As XElement In Records

    Try

        Dim Num090 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "090")
                  Select datafield.Elements.ElementAt(0))

        Dim DTIC856 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "856")
                    Select datafield).Skip(1).First()

        Dim Edocs856 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "856")
                   Select datafield)


        Dim Title245_A = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "245")
                   Select datafield.Elements.ElementAt(0))

        Dim Title245_B = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "245") And datafield.Elements.Count >= 2
               Select datafield.Elements.ElementAt(1))



        Dim Author100 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "100")
                   Select datafield.Elements.ElementAt(0))


        Dim ADAnum037 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "037")
                   Select datafield.Elements.ElementAt(0))


        Dim CoAuthor500 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "500")
                   Select datafield).Skip(2).First()

        Dim Quater500 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "500")
                    Select datafield).Skip(1).First()

        Dim BOSUNnum001 = (From controlfield In ser.Descendants("controlfield") Where (controlfield.Attribute("tag").Value = "001")
                    Select controlfield)

        Dim Type999 = (From datafield In ser.Descendants("datafield") Where (datafield.Attribute("tag").Value = "999")
                Select datafield.Elements.ElementAt(2)) '.Skip(0).First()

        strFinalString = Num090.Value & "  " & strPipe & " " & DTIC856.Value & "  " & strPipe & "  " & Edocs856.Value & "  " & strPipe & "  " & Title245_A.Value _
          & "  " & Title245_B.Value & "  " & strPipe & "  " & Author100.Value & "  " & strPipe & "  " & ADAnum037.Value & "  " & strPipe & "  " & CoAuthor500.Value & _
            "  " & strPipe & "  " & Quater500.Value & "  " & strPipe & "  " & BOSUNnum001.Value & "  " & strPipe & "  " & Type999.Value



        objWriter.WriteLine(strFinalString)



    Catch ex As Exception

    Finally

    End Try

Next


objWriter.Close()


MsgBox("Done writing to the file")

推荐答案

可能是最简单的方法,考虑到您已经编写的代码是声明一个Integer属性(称为BytesWritten之类)此属性的二传手有:

Probably the easiest way, given the code you have already written is to declare an Integer Property (call it something like BytesWritten) In the Setter for this property have:

_BytesWritten += Value '_BytesWritten is the member associated with the Property
ProgressBar1.Value = _BytesWritten


然后替换此行:


Then replace this line:

objWriter.WriteLine(strFinalString)


与:


with:

objWriter.WriteLine(strFinalString)
BytesWritten += strFinalString.Length



并完全摆脱do-loop



and get rid of your do-loop entirely


这篇关于LINQ to XML实现进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆