C# Simple XML Reading probmem

jazzIIIlove
jazzIIIlove used Ask the Experts™
on
Hi there;

I am a little embarrased to ask this. I have an xml file to read in C# but I have problems with it.

while(reader.Read())
           
                if (reader.NodeType == XmlNodeType.Text)
                {
                   
                    applicationName = reader.Value;
                    numberOfProcesses = Int32.Parse(reader.Value); //I fail here since reader.Value is applicationName, how can I grab NumberOfPrcesses in the xml file?

Best regards.
<?xml version="1.0" encoding="utf-8" ?>
<Application>
<ApplicationName>blabla</ApplicationName>
<NumberOfProcesses>2</NumberOfProcesses>
</Application>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Carl TawnSenior Systems and Integration Developer
Commented:
It's because the XmlReader reads one element at a time. You are currently trying to read two pieces of information from the same element.

You probably want something more along the lines of:
            string name;
            int count;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "ApplicationName")
                        applicationName = reader.ReadElementContentAsString();

                    if (reader.Name == "NumberOfProcesses")
                        numberOfProcesses = reader.ReadElementContentAsInt();
                }
            }

Open in new window

Commented:
You can try something like this, using the xml structure rather than reading it line by line.

It could be that
SelectNodes("Application")
should be
SelectNodes("/Application")
i always forget when to use it or not.

XmlDocument xmlDocument = new XmlDocument();
try
{
	xmlDocument.Load("yourxmlfile.xml");
	foreach(XmlNode node in xmlDocument.SelectNodes("Application")){
		applicationName = node.SelectSingleNode("ApplicationName").InnerText;
                numberOfProcesses = Int32.Parse(node.SelectSingleNode("NumberOfProcesses").InnerText)
	}
}catch{
	//do something if it failed
}

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

am writing project with you xml file and i will upload
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ReadXMLFile
{
    class Program
    {
        static void Main(string[] args)
        {


            XmlTextReader reader = new XmlTextReader( @"C:\Inetpub\wwwroot\ReadXMLFile\ReadXMLFile\ApplicationINFO.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 (reader.Value);
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        Console.Write("</" + reader.Name);
                        Console.WriteLine(">");
                        break;
                }
            }
            Console.ReadLine();
   





        }
    }
}
XmlDocument doc= new XmlDocument();
try
{
      doc.Load("XMLFILE.xml");
      foreach(XmlNode node in doc.SelectNodes("Application"))

           {
            AppName = node.SelectSingleNode("ApplicationName").InnerText;
                ProCount = Int32.Parse(node.SelectSingleNode("NumberOfProcesses").InnerText)
            }
}

catch

{
      
}
check this files with above solution
ApplicationINFO.xml
Program.cs

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial