Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-28
11
Medium Priority
?
657 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

618 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