Click here to Skip to main content
13,194,662 members (86,804 online)
Rate this:
Please Sign up or sign in to vote.
See more:
how to read particular record from xml file through id of that record using
Posted 30-Sep-12 20:29pm
lukeer 1-Oct-12 2:44am
What have you tried so far?
What problems / errors did you encounter?

I'd start with the XmlDocument[^] class and read through MSDN's articles and examples from there.
Sandeep Mewara 1-Oct-12 2:56am
And the issue is?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

For example, Consider the following XML file,

<?xml version="1.0" encoding="utf-8"?>
    <LastUpdated>09/06/2011 10:21:53</LastUpdated>
    <LastUpdated>09/06/2011 10:11:29</LastUpdated>
    <LastUpdated>21/06/2011 10:39:40</LastUpdated>
    <LastUpdated>19/01/2012 13:24:00</LastUpdated>
    <LastUpdated>29/03/2006 16:21:21</LastUpdated>
    <LastUpdated>19/01/2012 13:24:27</LastUpdated>

We are now looking for the xml record with rec_id = "111"

Enumerate through the XML file and read one by one,

//This is the main method, or the calling function,
string rec_id="111";
//Call the function to get the xml record for matching rec_id
XElement MatchingRecord= GetMatchingXML(rec_id);

private XElement GetMatchingXML(string RecID)
   XElement MatchingXML = null;
//Get the enumerator to traverse through each xml record  in the XML file
   IEnumerable <XElement> Records=
                        from el in XML.Elements("Record")
                        select el;
//Now traverse through each  xml record and look for matching rec_id
   foreach (XElement record in Records)
//Check if the provided rec_id is matching with the recId fro this particular xml record 
      if (RecID.Equals(record .Descendants("rec_id").FirstOrDefault().Value.ToString()))
//if matching, return this xml record
          MatchingXML = record ;
    return MatchingXML;

This code will return the following XML file,

    <LastUpdated>09/06/2011 10:11:29</LastUpdated>

Try this and ask me if you have any questions.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171018.2 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100