Results 1 to 5 of 5

Thread: [RESOLVED] XML parsing efficiency

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Resolved [RESOLVED] XML parsing efficiency

    Hi,

    I decided to put the settings for an application in an xml file.
    The format is as follows
    Code:
    ...
    <settings>
       <conf1>Value1</conf1>
       <conf2>Value2</conf2>
       <conf3 />
       <conf4>Value4</conf4>
    </settings>
    What I did before was reading through all elements and check the element name with a switch each time (with an xmltextreader).
    It works, but strikes to me as rather inefficient.
    I've been experimenting with xmldocument but I'm not getting very far.
    This is what I at the moment:
    Code:
    StreamReader sw = new StreamReader(Path, Encoding.Unicode);
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(sw);
    XmlElement baseElement = xdoc.DocumentElement["settings"];
    Any pointers?
    Thanks
    Delete it. They just clutter threads anyway.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: XML parsing efficiency

    Hmm. .NET 2 - that means you cannot use LINQ which is a shame.

    With the above method you would probably have to use a foreach loop to cycle over the element descendants. Here is something for you to read.
    My Blog.

    Ryan Jones.

  3. #3
    Lively Member
    Join Date
    Apr 2010
    Posts
    105

    Re: XML parsing efficiency

    Use SelectNodes() from the document with an XPath string and your life will become easy
    I wrote a book Visual Studio 2008 Programming
    Amazon.com / Visual Studio 2008 Programming / By Jamie Plenderleith & Steve Bunn

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: XML parsing efficiency

    I'd create a class, then use XML Serialization to get the settings in/out of it....then you don't even need to deal with XPath.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: XML parsing efficiency

    Another quick and dirty method would be to utilize the DataTable's Read/Write XML methods:
    Code:
          private void WriteSettings()
          {
             DataTable settings = new DataTable("Settings");
             settings.Columns.Add("Config1", typeof(string));
             settings.Columns.Add("Config2", typeof(string));
             // Etc..
    
             settings.Rows.Add(new object[] { 
                "Value1", 
                "Value2", 
                // Etc.. 
              });
    
             settings.WriteXml(@"C:\Tests\Settings.xml", XmlWriteMode.WriteSchema);
          }
    
          private void ReadSettings()
          {
             DataTable settings = new DataTable();
             settings.ReadXml(@"C:\Tests\Settings.xml");
             Debug.Print(settings.Rows[0]["Config1"].ToString());
             Debug.Print(settings.Rows[0]["Config2"].ToString());
             // Etc..
          }

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