How to update a global variable XmlTextWriter, from a local process in VB.NET?

LA RC 41 Reputation points
2022-04-12T04:33:30.877+00:00

I am trying to build an XML file, where I generate a variable type XmlTextWriter in the main class, to be able to read it globally. Then, inside a NavButton, I create a new variable with the same name and send it the path attribute so that it generates the file within a specific path.

When I get to the conditional if it is TRUE, it enters the function and sends 4 parameters, which the function receives correctly, but when it starts to write inside the XML, it does not send an error and continues fine, but when opening the XML already when finished, is empty and only with two lines.

I understand that you would be talking about two variables with the same name, but one global and one local and both have different paths.

My question is, how can I update the attribute or path of the global variable, from the local one and pass it the correct path so that the function writes in the XML that it should be?

This is my code:

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

Thank you for your time and attention.

Regards.

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-04-12T09:16:22.53+00:00

    Hi @LA RC ,
    In your code , sheetTest accesses global variables, while NavButton1 accesses local variables.
    You can make global variables public, and then initialize and assign values to them when you use them.
    Here are my test result and test code which you can refer to.
    192214-image.png

        Public writerC As XmlTextWriter  
        Private Sub NavButton1_Click(sender As Object, e As EventArgs) Handles NavButton1.Click  
            Dim pathRoot As String = "C:\Users\lijiachenl\source\repos\Case22.4.1\WindowsApp2\"  
            Dim strPathC As String = pathRoot & "\" & "e" & TextBox1.Text & ".XML"  
            writerC = 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")  
            Dim fs As New FileStream(strPathC, FileMode.Open, FileAccess.Read)  
            Dim data(fs.Length) As Byte  
            fs.Read(data, 0, fs.Length)  
            Console.WriteLine(Encoding.UTF8.GetChars(data))  
        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  
    

    Hope these could be helpful.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,381 Reputation points
    2022-04-12T13:34:34.83+00:00

    A couple of things.

    When working with paths use IO.Path.

    When working with XML you might find XElement easier to use.

            Dim pathRoot As String = "C:\Temp"  
            Dim strPathC As String  
            strPathC = IO.Path.Combine(pathRoot, "e", TextBox1.Text)  
            strPathC = IO.Path.ChangeExtension(strPathC, "xml")  
      
            Dim myXML As XElement  
            'using XML Literal  
            myXML = <PageCollection>  
                        <Page></Page>  
                        <Collection></Collection>  
                    </PageCollection>  
      
            myXML.Save(strPathC)  
            Stop 'look at myXML  
      
    

    You can even use literals with LINQ queries in embedded expressions,

            Dim pathRoot As String = "C:\Temp"  
            Dim strPathC As String  
            strPathC = IO.Path.Combine(pathRoot, "e", TextBox1.Text)  
            strPathC = IO.Path.ChangeExtension(strPathC, "xml")  
      
            Dim someVal() As String = {"A", "B", "C", "Z"}  
            Dim myXML As XElement  
            'using XML Literal  
            myXML = <PageCollection>  
                        <Page></Page>  
                        <Collection>  
                            <%= From v In someVal  
                                Where v <> "B"  
                                Select <Col><%= v %></Col>  
                            %>  
                        </Collection>  
                    </PageCollection>  
      
            myXML.Save(strPathC)  
            Stop 'look at myXML  
      
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.