Click here to Skip to main content
15,741,310 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi To all

I've been searching for this problem and seem not find a link that will help mw maybe its the key words for searching that i am typing wrong, well then here is my problem.

I have an xml file the has the following contents as below :

<?xml version="1.0" encoding="utf-8" ?> 
- <Employees>
- <Employee>
- <Employee>
- <Employee>
- <Employee>

Writing to an xml file i have no problem.
What I would like to do retrieve not the whole contents of the file, but retrieve only the one the employees searching by one attribute, say the employee attribute. If you were to assist me in tutorials that would help me over come my problem using C# i would be forever grateful.

Thanks in advance
Updated 2-Apr-13 21:06pm

Well, you have several possibilities:
1) Deserialize this to a c# collection. Load and save objects to XML using serialization[^], XML serialization using Generics[^]
2) Use XQuesy/XPath libraries: XSL 2.0 and XQuery 1.0 in .NET[^]
3) Use LINQ to XML:[^],[^]
4) You can even use regular expressions in some limited situations, like what you specified.
Share this answer
XmlDocument doc = new XmlDocument();
            XmlNodeList addlst = doc.SelectNodes("Employees/Employee/FirstName/firstName");
Share this answer
You might use XPath for the purpose. If you are not familiar with XPath queries, then have a look at w3schools XPath tutorial[^]. You may also find "World’s Smallest XML XPath Tutorial"[^] interesting.

The following silly example searches for the Employee 'Norah' and reports her details (warning: no error checking):
using System;
using System.Xml;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            XmlDocument doc = new XmlDocument();
            doc.Load("empl.xml"); // Your file
            XmlNode xnd = doc.DocumentElement.SelectSingleNode("./Employee[FirstName/text()='Norah']");
            Console.WriteLine("ID = {0}, LastName = {1}, Salary = {2}", xnd.ChildNodes[0].InnerText, xnd.ChildNodes[2].InnerText, xnd.ChildNodes[3].InnerText);
Share this answer

follow Jeson method.
you can get XPath description form XPath

if you need further help , let me know

Share this answer
sijimann 3-Apr-13 11:00am    
thanks a lot
codeninja-C# 4-Apr-13 3:35am    
you are welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900