?
Solved

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

Posted on 2004-10-28
11
Medium Priority
?
664 Views
Last Modified: 2011-10-03
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!
0
Comment
Question by:icompass
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 5

Expert Comment

by:Kelmen
ID: 12441327
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
 
LVL 13

Expert Comment

by:dungla
ID: 12442128
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
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 12445343
Why not create a data class to handle the information and serialiaze/deserialize it to/from xml?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 15

Expert Comment

by:SRigney
ID: 12446611
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
 

Author Comment

by:icompass
ID: 12446646
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
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 1000 total points
ID: 12446721
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12453981
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
 
LVL 13

Expert Comment

by:dungla
ID: 12460226
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
 
LVL 13

Expert Comment

by:dungla
ID: 12460267
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12512524
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
 

Author Comment

by:icompass
ID: 12524787
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses
Course of the Month4 days, 7 hours left to enroll

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question