Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through xml file with XmlTextReader and read data

Status
Not open for further replies.

Ranvier

Programmer
Jun 17, 2004
73
0
0
GB
Hello,

I have an .xml file that I am using as a configuration file and I want to read the data in the file and use the values in a condition in my logic.

This is my xml file:

<?xml version="1.0" encoding="utf-8" ?>
- <caseSettings xmlns:xsi=" xmlns:xsd="- <monitorTypes>
- <monitorType>
<typeID>1</typeID>
<monitortypeid>4</monitortypeid>
<eventype>15</eventype>
<action>OOH</action>
<sla>1</sla>
<supportlevel>1</supportlevel>
<serviceitem>9</serviceitem>
<subjectcontains>is offline</subjectcontains>
<alarmstage>Second</alarmstage>
</monitorType>
- <monitorType>
<typeID>2</typeID>
<monitortypeid>1</monitortypeid>
<eventype>2</eventype>
<action>OOH</action>
<sla>3</sla>
<supportlevel>4</supportlevel>
<serviceitem>5</serviceitem>
<subjectcontains>TEST message only</subjectcontains>
<alarmstage>first</alarmstage>
</monitorType>
</monitorTypes>
</caseSettings>

This is my c sharp code that reads the xml file

XmlTextReader reader = new XmlTextReader(@"c:\kConfig.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine("VALUE = " + reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
break;
}
}

All this works fine, but this lists all the data in the xml file and I need to know when a new section starts in the xml file so I can look at the elements in that section. For example <monitorType> is the start of the new section - how do I loop through the <monitorType>, query the elements in that section for my condition and then move to the next <monitorType> section.

Is this possible and how do I do it?

Thanks in advance
 
it appears that your xml is a collection of monitor types. so create an object called Monitor type with the corresponding properties. use xpath queries to pull the values from the xml.

it would look something like this
Code:
class MonitorTypeRepository
{
   public IEnumerable<MonitorType> FindAll()
   {
        var doc = new XmlDocument();
        doc.Load("path to xml");

        var nodes = doc.SelectNodes("//monitorType");
        foreach(var node in nodes)
        {
            yield return MapFrom(node);
        }
   }

   private MonitorType MapFrom(XmlNode node)
   {
        return new MonitorType
                    {
                        TypeId = int.parse(node.SelectNode("typeid").Value),
                        MonitoryTypeId = int.parse(node.SelectNode("monitortypeid").Value),
                        EventType = int.parse(node.SelectNode("eventtype").Value),
                        ...
                    };
   }
}

public class MonitorType
{
   public int TypeId {get;set;}
   public int MonitorTypeId {get;set;}
   public int EventType {get;set;}
}
then you can use the enumeration of MonitorTypes in code and not think about the xml.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks, this sounds exactly what I need. I'm not that hot with c sharp and don't get everything in the example you gave.

The MapFrom(node) is saying I have invalid arguments. It doesnt seem to like

TypeId = int.parse(node.SelectNode("typeID").Value)

Error:'int' does not contain a definition for 'parse'

Also how do I specify a string variable instead of an int

For the MapFrom(node) I also get the following error

Error Argument '1': cannot convert from 'object'

Thanks
 
c# is case sensitive so use Parse instead of parse. if Value returns an object then int.Parse won't work use Convert.ToInt32();

if value returns a string you don't have to do anything to convert it, otherwise use Convert.ToString();

some simple googling of the exception type and message is one of the first steps in debugging.



Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top