How to get records from XML based on input conditions [modified]
-
Hi, Please find the XML and code below.
<Table>
<Record>
<ORDERID>900132AB3</ORDERID>
<ITEMNAME>1234</ITEMNAME>
<QUANTITY>900132AB3</QUANTITY>
<PRICE>100</PRICE>
</Record>
<Record>
<ORDERID>900132CD4</ORDERID>
<ITEMNAME>564</ITEMNAME>
<UNITPRICE>1999</UNITPRICE>
<INVOICE>xxx</INVOICE>
</Record>
<Record>
<NAME>Gretchen Rivas</NAME>
<PHONE>206-555-0144</PHONE>
<NETWORTH>11</NETWORTH>
<PRICE>100</PRICE>
</Record>
</Table>class Attribute
{
string mName;
string mValue;
}Attribute item = new Attribute();
item.Name = "PRICE";
item.Value = "100";Attribute item = new Attribute();
item1.Name = "INVOICE";
item1.Value = "xxx";public void GetUniqueIds(List<Attribute> inputParams)
{
static XDocument uidDb = XDocument.Load(@Application.StartupPath + "\\test.xml");
//here I need to get the records which are having the values of inputParams list//I tried with the below expression to get the records, it worked..
//Here I hardcoded the indexes of inputParams(0,1..) But how to create the expression dynamically.List<XElement> lstRecords = null;
lstRecords = uidDb.Descendants("Record").Where(r =>
(r.Element(inputParams[0].Name).Value.Equals(inputParams[0].Value)&&
r.Element(inputParams[1].Name).Value.Equals(inputParams[1].Value))).ToList();}
Please let me know how to get the records from XML file which satisfies the inputParams list. Thanks in advance.
modified on Monday, October 4, 2010 9:03 AM
-
Hi, Please find the XML and code below.
<Table>
<Record>
<ORDERID>900132AB3</ORDERID>
<ITEMNAME>1234</ITEMNAME>
<QUANTITY>900132AB3</QUANTITY>
<PRICE>100</PRICE>
</Record>
<Record>
<ORDERID>900132CD4</ORDERID>
<ITEMNAME>564</ITEMNAME>
<UNITPRICE>1999</UNITPRICE>
<INVOICE>xxx</INVOICE>
</Record>
<Record>
<NAME>Gretchen Rivas</NAME>
<PHONE>206-555-0144</PHONE>
<NETWORTH>11</NETWORTH>
<PRICE>100</PRICE>
</Record>
</Table>class Attribute
{
string mName;
string mValue;
}Attribute item = new Attribute();
item.Name = "PRICE";
item.Value = "100";Attribute item = new Attribute();
item1.Name = "INVOICE";
item1.Value = "xxx";public void GetUniqueIds(List<Attribute> inputParams)
{
static XDocument uidDb = XDocument.Load(@Application.StartupPath + "\\test.xml");
//here I need to get the records which are having the values of inputParams list//I tried with the below expression to get the records, it worked..
//Here I hardcoded the indexes of inputParams(0,1..) But how to create the expression dynamically.List<XElement> lstRecords = null;
lstRecords = uidDb.Descendants("Record").Where(r =>
(r.Element(inputParams[0].Name).Value.Equals(inputParams[0].Value)&&
r.Element(inputParams[1].Name).Value.Equals(inputParams[1].Value))).ToList();}
Please let me know how to get the records from XML file which satisfies the inputParams list. Thanks in advance.
modified on Monday, October 4, 2010 9:03 AM
/* List<XElement> lstRecords = null; lstRecords = uidDb.Descendants("Record").Where(r => (r.Element(inputParams[0].Name).Value.Equals(inputParams[0].Value)&& r.Element(inputParams[1].Name).Value.Equals(inputParams[1].Value))).ToList(); */ var lstRecords1 = from r in uidDb.Descendants("Record") from inattr in inputParams where r.Element(inattr.Name) != null && r.Element(inattr.Name).Value == inattr.Value select r; lstRecords = (lstRecords1.Count() > 0) ? lstRecords1.ToList<XElement>() : null; Note: which is not optimized one but still u can use it
-
/* List<XElement> lstRecords = null; lstRecords = uidDb.Descendants("Record").Where(r => (r.Element(inputParams[0].Name).Value.Equals(inputParams[0].Value)&& r.Element(inputParams[1].Name).Value.Equals(inputParams[1].Value))).ToList(); */ var lstRecords1 = from r in uidDb.Descendants("Record") from inattr in inputParams where r.Element(inattr.Name) != null && r.Element(inattr.Name).Value == inattr.Value select r; lstRecords = (lstRecords1.Count() > 0) ? lstRecords1.ToList<XElement>() : null; Note: which is not optimized one but still u can use it
Thanks for your reply. I tried your expression, it returned duplicate records. I tried the below, which is working.. Thank you once again for your post.
lstRecords = uidDb.Descendants("Record").Where(r =>ApplyFilter(r, inputParams)).ToList();
private bool ApplyFilter(XElement record, List<Attribute> iParams)
{
foreach (Attribute param in iParams)
{
if (record.Element(param.Name).Value != param.Value)
return false;
}
return true;
}