For example, Consider the following XML file,
="1.0"="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,
string rec_id="111";
XElement MatchingRecord= GetMatchingXML(rec_id);
private XElement GetMatchingXML(string RecID)
{
XElement MatchingXML = null;
IEnumerable <XElement> Records=
from el in XML.Elements("Record")
select el;
foreach (XElement record in Records)
{
if (RecID.Equals(record .Descendants("rec_id").FirstOrDefault().Value.ToString()))
{
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.