Results 1 to 15 of 15

Thread: Parsing an XML file

Hybrid View

  1. #1
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Parsing an XML file

    I find it much easier to use XElement and axis properties to read XML files compared to using XmlDocument.
    Code:
        Dim xml = XElement.Load("c:\Settings.xml")
        Dim setting1 = xml...<Setting1>.Value
        MsgBox(setting1)

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Parsing an XML file

    There's no one "right" answer to this question, but I can outline the answers and let you pick one

    There's two categories of XML parsers I'm aware of, I call them DOM and SAX parsers.

    SAX parsers are named based on one of the first parsers to use this technique. It's more like how you read a text file line by line, only a SAX parser reads node by node. .NET's version of this kind of parser is the XmlReader, and it's kind of strange compared to the traditional implementation. In the traditional implementations, you call some kind of Read() method that reads a node, then the parser raises an event that represents what kind of node was read. For example, the first few events raised in your example document might be DocumentRoot, Comment, StartElement, StartElement. .NET's parser doesn't raise the events, instead you use properties and methods to determine what node you're on and extract data. To get the value of the Setting1 element, you'd have to write code like this:
    Code:
    Dim reader As XmlReader = XmlReader.Create("test.xml")
    Dim setting1 As String
    
    While reader.Read()
        If reader.NodeType = XmlNodeType.Element Then
            If reader.Name = "Setting1" Then
                setting1 = reader.ReadElementContentAsString()
            End If
        End If
    End While
    DOM parsers read the entire XML file and form a document model in memory. This allows you to use a special language called XPath to query the file for information quickly. XPath is too big to discuss in this answer, but tutorials exist. The workflow for a DOM parser is much different; you load the file, then make queries. Here's the code that would fetch the value of the Setting1 element:
    Code:
    Dim setting1 As String
    
    Dim doc As New XmlDocument
    doc.Load("test.xml")
    
    Dim setting1Node As XmlNode = doc.SelectSingleNode("//Setting1")
    setting1 = setting1Node.InnerText
    Joacim Andersson is pointing you to XElement, which is related to a different DOM parser that was introduced in (I think) VS 2008. It comes with a neat inline syntax for working with the DOM in VB .NET. You'll have to lean on him for more explanation; I work mostly in C# and thus I'm more familiar with XmlDocument and XmlReader.

    Why are there so many choices? Each has tradeoffs.

    DOM parsers obviously require less code and make it easier to get to a particular element in a file. DOM parsers can read anything in the file at any time; SAX parsers can only read one-way so if you need to get something from earlier in the file you have to start over from the beginning. SAX parsers read one element at a time and thus use very little memory; DOM parsers must hold the entire DOM in memory. XPath lets you do some neat queries against the file, so it can be easier to pull weirdo stuff from random places.

    So for small files or files that require some kind of complicated querying, I usually use the DOM parser XmlDocument. If I start to notice the load time, I have to switch to a SAX parser, and I usually end up building a data structure that simulates what I'd be doing with the DOM parser anyway. I can't tell you what'd be best for your solution, but I have a hunch based on your example that one of the DOM parsers will be fine. For redundancy, don't assume since I didn't talk about XElement I disagree with Joacim Andersson's post; I don't have enough experience with XElement to have an opinion on that technique.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width