Jackass03
asked on
Accessing XML Element in C#
Hi,
I have an xml file in the following structure:
<Jobs>
<Job ID="20"><CategoryID>4</Cat egoryID></ 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?
I have an xml file in the following structure:
<Jobs>
<Job ID="20"><CategoryID>4</Cat
</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("/
----
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It also outputs the error as a FormatException - Input string was not in the correct format.
ASKER
Magic mate - works a treat
ASKER
string sXMLFile = "xml\\jobs.xml";