Link to home
Start Free TrialLog in
Avatar of Jackass03
Jackass03

asked on

Accessing XML Element in C#

Hi,

I have an xml file in the following structure:

<Jobs>
<Job ID="20"><CategoryID>4</CategoryID></Job >
</Jobs>

I would like to access the relevant Job ID (depending on the value passed in) and then store the CategoryID into a variable.

So here is my code.

I'm using the System.Xml namespace and passing in a JobID in 'nJobID' variable.

----
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sXMLFile);

sCategoryID = xmlDoc.SelectSingleNode("/Jobs/Job[@ID = " + nJobID + "]/CategoryID");
----

When I have output the sCategoryID value it has returned 'System.Xml.XmlElement' rather than a value such as 4.

Any ideas?  Am i accessing the xml node incorrectly?




Avatar of Jackass03
Jackass03

ASKER

sXMLFile is set as:

string sXMLFile = "xml\\jobs.xml";
ASKER CERTIFIED SOLUTION
Avatar of Ramuncikas
Ramuncikas
Flag of Lithuania 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
It also outputs the error as a FormatException - Input string was not in the correct format.
Magic mate - works a treat