C# XML The most efficient way of loading a XML file

I have my XML file:

<ServerConnection>Network Library=My_SQLDB_STRING</ServerConnection>
<ServerConnectionTimeout>Connect Timeout=2;</ServerConnectionTimeout>
<ItemDB>
    <Server Title="t1">test1</Server>
    <Server Title="t2">test2</Server>
    <Server Title="t3">test3</Server>
    <Server Title="t4">test4</Server>
    <Server Title="t5">test5</Server>
</ItemDB>

There are x amounts of <Server> elements within ItemDB.

I want to capture the ServerConnection and ServerConnectionTimeout in 2 seperate Strings,

Then create an arraylist of all <server>'s that are under ItemDB.

I'm tossing around XML DOM or the XML reader, but I want it efficient so it doesn't slow our live servers down. This is small app that runs behind the scenes to query tables to see if email reminders must be sent out, and queries the DB's every minute to see if there's a reminder at that time.

Thanks!
icompassAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KelmenCommented:
XmlReader should be your pick.

As stated in the MSDN:

Represents a reader that provides fast, non-cached, forward-only access to XML data.

I would use the DOM if the xml and the data volume won't penalize the perf. And if the loaded xml will be reuse, DOM will give better throughput.

DOM - volume not huge, reuse

* Reuse = it will be used for process few/more times in one run

XmlReader - huge volume, 1 time process.
0
dunglaCommented:
I think maybe you should try XMLTextReader, simply like that

public static void ReadXML(out string ServerConnection, out string ConnectionTimeOut, out ArrayList Servers)
            {
                  ServerConnection = "";
                  ConnectionTimeOut = "";
                  Servers = new ArrayList();

                  XmlTextReader reader = new XmlTextReader(@"D:\test.xml");
                  while(reader.Read())
                  {
                        reader.MoveToElement();
                        if (reader.Name == "ServerConnection")
                        {
                              ServerConnection = reader.ReadInnerXml();
                        }
                        if (reader.Name == "ServerConnectionTimeOut")
                        {
                              ConnectionTimeOut = reader.ReadInnerXml();
                        }
                        if (reader.Name == "Server")
                        {
                              Servers.Add(reader.ReadInnerXml());
                        }
                  }
0
NipNFriar_TuckCommented:
Why not create a data class to handle the information and serialiaze/deserialize it to/from xml?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

SRigneyCommented:
As long as we're going to throw all options out here.  Why don't you use ADO.Net dataset to read it with ReadXML.  Then the array, will already be set up in a table and you can loop through it easily.
0
icompassAuthor Commented:
This is for reading only, I don't need to write anything.

I tried dungla's way, but that gave me a lot of errors after it parsed the ServerConnection.
I can get the ServerConnection and ConnectionTimeOut, but I'm not 100% fluent in C# with XML yet and I don't really know how to loop through the

<ItemDB>
    <Server Title="t1">test1</Server>
    <Server Title="t2">test2</Server>
    <Server Title="t3">test3</Server>
    <Server Title="t4">test4</Server>
    <Server Title="t5">test5</Server>
</ItemDB>

part to get everything.
0
NipNFriar_TuckCommented:
there are a couple of ways that you can do this...

one you can loop through nodes... easy to do but slow...

foreach ( XmlNode n in XmlDoc.ChildNodes ) {
   switch ( XmlNode.Name ) {
      case "ServerConnection":
          // do stuff
          string sConn = n.Value.ToString();
          break;
      case "ConnectionTimeOut":
          // do stuff
          string sTimeOut = n.Value.ToString();
          break;
      case "ItemDB":
          // do stuff
          foreach ( XmlNode cn in n.ChildNodes ) {
               // Do more stuff
               string sTitle = cn.Value.ToString();
          }
          break;
   }
}

The other you can use XPath... here is a site to explain that...

http://www.zvon.org/xxl/XPathTutorial/Output/example1.html

Hope this helps...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AvonWyssCommented:
DOM is slow, Datasets are slower. If you are looking for performance, this is not an option.

XmlTextReader as shown by Dungla would be my pick also. Note that you can also read the inner data of the Server node easily using the MoveToAttribute(name) method.

Note that the XML file you posted is invalid, it lacks a root XML node.
0
dunglaCommented:
icompass,

Can you show me the errors message? I have tested the code, the function ReadXML() receive 3 params as out param which met your expectation, i think so.

Here is my code
==== MyClass.cs ====
using System;
using System.Xml;
using System.Collections;
class MyClass
{
 static void Main(string[] args)
 {
   string ServerConnection;
   string ConnectionTimeOut;
   ArrayList Servers;
  ReadXML(out ServerConnection, out ConnectionTimeOut, out Servers);
  Console.WriteLine("ServerConnection = {0}", ServerConnection);
  Console.WriteLine("ConnectionTimeOut = {0}", ConnectionTimeOut);
  for (int j = 0; j < Servers.Count; j++)
  {
     Console.WriteLine("Server {0} = {1}", j.ToString(), Servers[j].ToString());
  }
  Console.ReadLine();
 }
 public static void ReadXML(out string ServerConnection, out string ConnectionTimeOut, out ArrayList Servers)
{... code above}
}
======== end of MyClass ====

I tested and have no errors while trying to read through the XML file and get all the node. Just try it again.

If you till receive errors, post here, it's might helpful for me to help you

Hope this help!

.:: LDLP ::.
0
dunglaCommented:
icompass,

In my code, when met the node name "Server" as in your XML file, get the value of the node and put into an array list. If you want to get the attribute of "Server" node, use AttributeCount, MoveToAttribute(string name), GetAttribute(string name) method to get all the attribute.

Hope this help
.:: LDLP ::.
0
AvonWyssCommented:
You were asking for the most EFFICIENT way to load the file. The question you picked may be the most CONVENIENT, but it's certainly not the most EFFICIENT. This is not very fair for the experts like kelmen or dungla which actually gave you the correct answer for your specific question.
0
icompassAuthor Commented:
AvonWyss,

Yes I was asking for the most Efficient way, and I do not doubt that Kelmen and Dungla are excellent programmers. However I am restrained by time as well, and the solution I picked was the one using the XMLDocument. It was the only one I was able to get to work with my XML file.

I've never played with XML before, and I'm a native VB coder switching to C#.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.