Share via

XmlTextReader

OSVBNET 1,401 Reputation points
2022-08-05T12:57:00.617+00:00

Hello
I've this sample to read and pass an xml document to a function:

Dim reader As XmlTextReader = Nothing
reader = New XmlTextReader(File.OpenRead("D:\file.xml"))
reader.WhitespaceHandling = WhitespaceHandling.None
pass reader to the function...
If Not reader Is Nothing Then reader.Close()

I've got 2 issues with this,
1/ Need to keep a copy of the whole XML document INTACT in a variable, what kind of variable can hold XML? I don't need any processing/parsing except this switch: WhitespaceHandling.None
Return no Whitespace and no SignificantWhitespace nodes.

2/ I will need to get the XML back and pass to the said function as XmlReader, the point is that once I read the file File.OpenRead("D:\file.xml") will need to close it and won't have access to it anymore, so must keep it in a variable.
Thanks for helping out :)

Developer technologies | VB
0 comments No comments

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    2022-08-08T06:50:02.97+00:00

    Hi @OSVBNET ,
    You can use the XmlDocument Class to save a copy of the xml file.

        NotInheritable Class XmlDocumentSample  
      
            Private Sub New()  
      
            End Sub  
      
            Shared reader As XmlReader  
            Shared filename As String = "bookdtd.xml"  
      
            Public Shared Sub Main()  
      
      
                Dim eventHandler As New ValidationEventHandler(AddressOf XmlDocumentSample.ValidationCallback)  
      
                Try  
      
                    ' Create the validating reader and specify DTD validation.  
                    Dim settings As New XmlReaderSettings()  
                    settings.DtdProcessing = DtdProcessing.Parse  
                    settings.ValidationType = ValidationType.DTD  
                    AddHandler settings.ValidationEventHandler, eventHandler  
      
                    reader = XmlReader.Create(filename, settings)  
      
                    ' Pass the validating reader to the XML document.  
                    ' Validation fails due to an undefined attribute, but the   
                    ' data is still loaded into the document.  
                    Dim doc As New XmlDocument()  
                    doc.Load(reader)  
                    Console.WriteLine(doc.OuterXml)  
                  
                Finally  
      
                    If Not (reader Is Nothing) Then  
                        reader.Close()  
                    End If  
      
                End Try  
      
            End Sub  
      
            ' Display the validation error.  
            Private Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)  
                Console.WriteLine("Validation error loading: {0}", filename)  
                Console.WriteLine(args.Message)  
            End Sub  
      
        End Class  
    

    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.

    0 comments No comments

Your answer

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