Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how to read particular record from xml file through id of that record using C#.net
Posted 30-Sep-12 21:29pm
Comments
lukeer at 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 at 1-Oct-12 2:56am
   
And the issue is?

1 solution

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

Solution 1

For example, Consider the following XML file,
 
<?xml version="1.0" encoding="utf-8"?>
<Records>
  <Record>
    <RecordID>8119</RecordID>
    <ID>AM5715</ID>
    <LastUpdated>09/06/2011 10:21:53</LastUpdated>
  </Record>
  <Record>
    <RecordID>111</RecordID>
    <ID>AM5716</ID>
    <LastUpdated>09/06/2011 10:11:29</LastUpdated>
  </Record>
  <Record>
    <RecordID>8121</RecordID>
    <ID>AM5717</ID>
    <LastUpdated>21/06/2011 10:39:40</LastUpdated>
  </Record>
  <Record>
    <RecordID>8123</RecordID>
    <ID>AM5718</ID>
    <LastUpdated>19/01/2012 13:24:00</LastUpdated>
  </Record>
  <Record>
    <RecordID>8126</RecordID>
    <ID>AM5719</ID>
    <LastUpdated>29/03/2006 16:21:21</LastUpdated>
  </Record>
  <Record>
    <RecordID>8127</RecordID>
    <ID>AM5720</ID>
    <LastUpdated>19/01/2012 13:24:27</LastUpdated>
  </Record>
</Records>
 
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 ;
          break;
      }
    }
        
    return MatchingXML;
}
 
This code will return the following XML file,
 
<Record>
    <RecordID>111</RecordID>
    <ID>AM5716</ID>
    <LastUpdated>09/06/2011 10:11:29</LastUpdated>
  </Record>
 
Try this and ask me if you have any questions.
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
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