0

Which one is the best XmlDocument or XmlReader (in performance) for parsing quite large XML files (size or number of elements in it)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
MrCSharp
  • 1,143
  • 17
  • 28

2 Answers2

3

The question shouldn't be which is faster but which is good for your case.

XmlDocument loads entire document into memory and allows you to modify it and query the content. After all you can save modified document back to file.

XmlReader provides read only and forward only access to the content of XML document, one element at the time.

You have to choose which description fits into your case.

You should also be aware that there is another way to handle XML documents in .NET, called LINQ to XML.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • 1
    So if i want to edit or add more elements to an XML file i will have to load all in the Memory ? Can i use XML to LINQ to edit XML files? – MrCSharp Oct 21 '13 at 06:55
  • Yes, you can. And I would really advice you to do so, because it's much easier to use than old XmlDocument. – MarcinJuraszek Oct 21 '13 at 06:58
  • And XmlDocument looses line numbers (e. g. useful for xsd schema validation) – jifb Jun 18 '21 at 09:39
-1

Another option maybe to load the XML into a dataset:

Public Function GetXMLDataset() As DataSet
    Try
        Dim ds As New DataSet
        Dim xd As New XmlDocument
        xd.LoadXml(txtXML.Text)
        ds.ReadXml(New XmlNodeReader(xd), XmlReadMode.InferSchema)
        Return ds
    Catch ex As Exception
        MsgBox(ex.Message)
        Return Nothing
    End Try
End Function

In this snippet the XML was already read into a textbox but you can develop other ways to get the XML.

You might want to define "quite larger" as reading the entire file may not be practical.

rheitzman
  • 2,247
  • 3
  • 20
  • 36