[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Accessing XML Element in C#

Posted on 2006-05-04
4
Medium Priority
?
391 Views
Last Modified: 2013-11-19
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?




0
Comment
Question by:Jackass03
  • 3
4 Comments
 

Author Comment

by:Jackass03
ID: 16603592
sXMLFile is set as:

string sXMLFile = "xml\\jobs.xml";
0
 
LVL 14

Accepted Solution

by:
Ramuncikas earned 200 total points
ID: 16603630
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sXMLFile);

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

myVar = sCategoryID.innerText
0
 

Author Comment

by:Jackass03
ID: 16603641
It also outputs the error as a FormatException - Input string was not in the correct format.
0
 

Author Comment

by:Jackass03
ID: 16603678
Magic mate - works a treat
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question