Click here to Skip to main content
15,881,882 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
<?xml version="1.0" encoding="utf-8" ?> 
- <Employees>
- <Employee>
  <ID>1</ID> 
  <FirstName>David</FirstName> 
  <LastName>Smith</LastName> 
  <Salary>10000</Salary> 
  </Employee>
- <Employee>
  <ID>3</ID> 
  <FirstName>Mark</FirstName> 
  <LastName>Drinkwater</LastName> 
  <Salary>30000</Salary> 
  </Employee>
- <Employee>
  <ID>4</ID> 
  <FirstName>Norah</FirstName> 
  <LastName>Miller</LastName> 
  <Salary>20000</Salary> 
  </Employee>
- <Employee>
  <ID>12</ID> 
  <FirstName>Cecil</FirstName> 
  <LastName>Walker</LastName> 
  <Salary>120000</Salary> 
  </Employee>
  </Employees>


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
Posted
Updated 2-Apr-13 21:06pm
v2

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: http://msdn.microsoft.com/en-us/library/bb387098.aspx[^], http://code.msdn.microsoft.com/windowsdesktop/LINQ-LINQ-to-XML-using-6f2d2218[^]
4) You can even use regular expressions in some limited situations, like what you specified.
 
Share this answer
 
C#
XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(@"XMLFile1.xml"));
            XmlNodeList addlst = doc.SelectNodes("Employees/Employee/FirstName/firstName");
 
Share this answer
 
v2
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):
C#
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
 
v4
hi,

follow Jeson method.
you can get XPath description form XPath

if you need further help , let me know

Regards,
--SJ
 
Share this answer
 
Comments
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