none
Get xml value by attribute value using linq RRS feed

  • Question

  •     <?xml version="1.0" encoding="utf-8" ?>
        <Logs>
        <Actions name="a1">
        <Action id="SignIn">1</Action>
        <Action id="SignOut">2</Action>
        <Action id="Open">3</Action>
        </Actions>
        <Actions name="a2">
        <Action id="SignIn">5</Action>
        <Action id="SignOut">6</Action>
        <Action id="Open">7</Action>
        </Actions>
        <Actions name="a3">
        <Action id="SignIn">8</Action>
        <Action id="SignOut">9</Action>
        <Action id="Open">10</Action>
        </Actions>
        </Logs>
        
        
        //Here i have one List of Id
        
        XElement xx=XElement.Load("abc.xml");
        
        string id="5";
        
        var vAct=from p in abc.Element(Logs).Elements(Actions)
        where p.Attribute("id").Value=="SignIn" && 
        (p.Attribute("id").Value).Value=id
        select p;
        
        
        //I want the output like :  
        
        <Actions name="a2">
        <Action id="SignIn">5</Action>
        <Action id="SignOut">6</Action>
        <Action id="Open">7</Action>
        </Actions>
        
        //Thanks in advance,Please help how to get the Collections
        based on where condition matching id=5
    Friday, November 30, 2012 3:15 AM

Answers

  • Dev Coder :

    Does this help ?

      XDocument objDoc = XDocument.Load(@"C:\Sites\stringEx\abc.xml");

                var result = from p in objDoc.Descendants("Logs").Descendants("Actions")
                             where ( (p.Element("Action").Value == "5" ) && (p.Element("Action").Attribute("id").Value == "SignIn"))
                             select p;
                Console.WriteLine(result.Count());
                foreach (var x in result)
                {
                    Console.WriteLine(x);
                }
                 

    • Marked as answer by Dev Coder Sunday, December 2, 2012 11:24 AM
    Friday, November 30, 2012 9:22 PM
  • DevCoder :

    Please try this .

       var result = from p in objDoc.Descendants("Logs").Descendants("Actions")
                             from q in p.Elements("Action")
                             where q.Attribute("id").Value == "Open" && q.Value == "7"
                             select p;
                foreach (var x in result)
                {
                    Console.WriteLine(x);
                }        

    • Marked as answer by Dev Coder Wednesday, December 5, 2012 1:41 AM
    Tuesday, December 4, 2012 1:03 PM

All replies

  • Dev Coder :

    Does this help ?

      XDocument objDoc = XDocument.Load(@"C:\Sites\stringEx\abc.xml");

                var result = from p in objDoc.Descendants("Logs").Descendants("Actions")
                             where ( (p.Element("Action").Value == "5" ) && (p.Element("Action").Attribute("id").Value == "SignIn"))
                             select p;
                Console.WriteLine(result.Count());
                foreach (var x in result)
                {
                    Console.WriteLine(x);
                }
                 

    • Marked as answer by Dev Coder Sunday, December 2, 2012 11:24 AM
    Friday, November 30, 2012 9:22 PM
  • Thanks Venkat, I got the excepted, it really works.

    Thanks again

    Sunday, December 2, 2012 11:27 AM
  •  <?xml version="1.0" encoding="utf-8" ?>
        <Logs>
        <Actions name="a1">
        <Action id="SignIn">1</Action>
        <Action id="SignOut">2</Action>
        <Action id="Open">3</Action>
        </Actions>
        <Actions name="a2">
        <Action id="SignIn">5</Action>
        <Action id="SignOut">6</Action>
        <Action id="Open">7</Action>
        </Actions>
        <Actions name="a3">
        <Action id="SignIn">8</Action>
        <Action id="SignOut">9</Action>
        <Action id="Open">10</Action>
        </Actions>
        </Logs>
        
        
        //Here i have one List of Id
        
        XElement xx=XElement.Load("abc.xml");
        
        string id="7";
        
        var vAct=from p in abc.Element(Logs).Elements(Actions)
        where p.Attribute("id").Value=="Open" && 
        (p.Attribute("id").Value).Value=id
        select p;
        
        
        //I want the output like :  
        
        <Actions name="a2">
        <Action id="SignIn">5</Action>
        <Action id="SignOut">6</Action>
        <Action id="Open">7</Action>
        </Actions>
        

    Here I am getting the above output as the code given by Venkat if  i take id="SignIn" && Value="5"

    But i take id="Open" && Value="7" then i didnot get the output.

    //Thanks in advance,Please help how to get the Collections
        based on where condition matching with  id="Open" && Value="7"

    Please help.....

    Monday, December 3, 2012 7:41 AM
  • DevCoder :

    Please try this .

       var result = from p in objDoc.Descendants("Logs").Descendants("Actions")
                             from q in p.Elements("Action")
                             where q.Attribute("id").Value == "Open" && q.Value == "7"
                             select p;
                foreach (var x in result)
                {
                    Console.WriteLine(x);
                }        

    • Marked as answer by Dev Coder Wednesday, December 5, 2012 1:41 AM
    Tuesday, December 4, 2012 1:03 PM
  • Thanks Venkat,

    It really works.....

    Thanks again...

    Wednesday, December 5, 2012 1:41 AM