Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How to get records from XML based on input conditions [modified]

How to get records from XML based on input conditions [modified]

Scheduled Pinned Locked Moved LINQ
xmltutorial
3 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarVish
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • N NarVish

      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

      M Offline
      M Offline
      Murugesan G
      wrote on last edited by
      #2

      /* 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

      N 1 Reply Last reply
      0
      • M Murugesan G

        /* 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

        N Offline
        N Offline
        NarVish
        wrote on last edited by
        #3

        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;
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups