Link to home
Start Free TrialLog in
Avatar of freename
freenameFlag for United States of America

asked on

Find element by name

Okay, XML folks,

How do I parse through Xml file to locate a specific node. The xml file is configuration information taken from a Windows form. The information is displayed in TabPages to the user. The configuration information is written to a Config Class with public properties. The public properties get their value from the xml file.

The first level tag is constant. It will always have the value "configuration". The second level tag can change based on the TabPage Tag value. The tags, property, name, value, type will always be there.

I have the following xml example:
<configuration>   // This remains constant
   <DBSettings>   // This element name is not constant. It changes based on TabPage Tag value
      < property>   // This is constant
         <name>DBServer<\name>  // element name is constant. contains the name of a Classes property
         <value>ServerName<\value> // element name is constant. contains the value of the previous property name
         <type>System.String<\type> // element name is constant. Contains the system type for property
      <\property>
      <property>
         <name>Retain<\name>
         <value>9<\value>
         <type>System.Int32<\type>
      <\property>
   <\DBSettings>
   <OtherSettings>
      <property>
         <name>LogDirectory<\name>
         <value>C:\Logs<\value>
         <type>System.String<\type>
      <\property>
   <\OtherSettings>
<\configuration>

There will be multiple properties entries between the second level tags. And like I said, the second level tags may change their name. I need to get the value of the LogDirectory for example.
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of freename

ASKER

anarki,
Thanks for your reply. Will you please explain your second sentence in your reply "...look for an element if it has name like 'LogDirectory'"? I'm not new to C# but new to XML. Anything you can suggest is gladly accepted.

You know, how xml document is tructured depends on many factors.
If I'd create a config file I'd probably go with the following structure:

<configuration>  
   <DBSettings>  
      <DBServer>    
         <value>ServerName<\value>
         <type>System.String<\type>
      <\DBServer>
      <Retain>
         <value>9<\value>
         <type>System.Int32<\type>
      <\Retain>
   <\DBSettings>
   <OtherSettings>
      <LogDirectory>
         <value>C:\Logs<\value>
         <type>System.String<\type>
      <\LogDirectory>
   <\OtherSettings>
<\configuration>

or even (almost equivalent to previous)

<configuration>  
   <DBSettings>  
      <DBServer value = "ServerName" type = "System.String"/>    
      <Retain value = "9" type = "System.Int32"/>
   <\DBSettings>
   <OtherSettings>
      <LogDirectory value=="C:\Logs" type="System.String"/>
   <\OtherSettings>
<\configuration>

In this caseit would be very easy to find <LogDirectory> element
by calling GetElementsByTagName and checking very first element in the returned list.

However I don't know how much you need to stick to the structure you are using now.
And may be it is better then mine.

Anyway I believe my approach should work. And if xml file is not big it's fast.
anarki,
Thank you very much for your reply. It makes a lot of sense. I came up with the earlier format myself. However, I know nothing about XML. Your input is a definite plus.