我正在嘗試構建一個 XML 檔案,在其中我在主類中生成一個變數型別 XmlTextWriter,以便能夠全域讀取它。然后,在 NavButton 中,我創建了一個具有相同名稱的新變數并將路徑屬性發送給它,以便它在特定路徑中生成檔案。
當我到達條件如果它是 TRUE 時,它進入函式并發送 4 個引數,函式正確接收,但是當它開始在 XML 中寫入時,它不會發送錯誤并繼續正常,但是當打開XML 已經完成,是空的,只有兩行。
我知道您會談論兩個具有相同名稱的變數,但一個全域變數和一個本地變數,并且它們都有不同的路徑。
我的問題是,如何從本地更新全域變數的屬性或路徑并將其傳遞給正確的路徑,以便函式在 XML 中寫入它應該是的?
這是我的代碼:
Public Class Form1
Dim path As String = "0"
Dim writerC As New XmlTextWriter(path, System.Text.Encoding.UTF8)
End Class
Private Sub NavButton1_ElementClick(sender As Object, e As NavElementEventArgs) Handles NavButton1.ElementClick
Dim pathRoot as String = "C:\Temp"
Dim strPathC As String = pathRoot & "\" & "e" & TextBox1.Text & ".XML"
Dim writerC As New XmlTextWriter(strPathC, System.Text.Encoding.UTF8)
writerC.WriteStartDocument(True)
writerC.Formatting = Formatting.Indented
writerC.Indentation = 2
writerC.WriteStartElement("PageCollection") '---PageCollection---
if i = 0 then
sheetTest("A", "B", "C", "D")
end if
writerC.WriteEndElement() '---PageCollection/---
writerC.Close() '---CLOSING XML FILE/---
pathRoot = pathRoot & "\" & "SentFiles"
Directory.CreateDirectory(pathRoot)
MsgBox("Successfully generated files.", MsgBoxStyle.Information MessageBoxButtons.OK, "TEST")
End Sub
Public Function sheetTest(firstParameter as string, secondParameter as string, thirdParameter as string, fourthParameter as string)
writerC.WriteStartElement("Page") '---Page---
writerC.WriteStartElement("Collection") '---Collection---
writerC.WriteEndElement() '---Collection/---
writerC.WriteEndElement() '---Page/---
End Function
謝謝你的時間。問候。
uj5u.com熱心網友回復:
由于變數范圍的作業原理,您宣告了兩個具有相同名稱(writerC)但范圍不同的不同變數。
試試這個:
Public Class Form1
Dim path As String = "0"
Dim writerC As XmlTextWriter
End Class
Private Sub NavButton1_ElementClick(sender As Object, e As NavElementEventArgs) Handles NavButton1.ElementClick
writerC = New XmlTextWriter(strPathC, System.Text.Encoding.UTF8)
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/459311.html
上一篇:ByVal表現得像ByRef
下一篇:參考msgbox上的按鈕
