none
Linq confusion - Nube question RRS feed

  • Question

  • Hi

    I'm just starting to look at LINQ to XML to access information from a web service. I have the code below:

    var z = from c in xdoc.Descendants("content")
    where c.Value == "Asbestos" && c.ElementsAfterSelf("item").Count<XElement>() > 0
    select new
    {
         xnode = c.ElementsAfterSelf("item").Descendants("content").Elements("name")
    };

    When I run the code, and expand the results views for z, I get the following:

    <name class="Document" icon="Images/Icons/Document.png" link="Asbestos">http://site.com?docID=1">Asbestos document A 21/01/2014</name>
    <name class="Document" icon="Images/Icons/Document.png" link="Asbestos">http://site.com?docID=2Asbestos">">Asbestos Image 1 21/01/2014</name>
    <name class="Document" icon="Images/Icons/Document.png" link="Asbestos">http://site.com?docID=3Asbestos">">Asbestos document B 21/01/2014</name>

    This does contain the data I want, but I want to have it as a data table so I can bind it to a gridview or similar as below:

    Type                  link                                          filename

    Document          http://site.com?docID=1           Asbestos document A 21/01/2014

    Document          http://site.com?docID=2Asbestos">           Image 1 21/01/2014

    Document          http://site.com?docID=3           Asbestos document B 21/01/2014


    Any help appreciated.

    Thanks

    Thursday, January 23, 2014 11:46 AM

Answers

  • +1 Vote for Molku..

    Here is enhancement to get DataTable as output. Hope this helps as start..

     string nodes = @"<Root><name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=1>Asbestos""> document A 21/01/2014</name>
     <name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=2Asbestos"">Asbestos Image 1 21/01/2014</name>
     <name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=3Asbestos"">Asbestos document B 21/01/2014</name></Root>";
    
            DataTable objTable = new DataTable();
            objTable.Columns.Add("Type", typeof(string)); objTable.Columns.Add("link", typeof(string)); 
            objTable.Columns.Add("filename", typeof(string));
           
            XDocument objDoc = XDocument.Parse(nodes); // XDocument.load("your xml file");
            foreach (var el in objDoc.Descendants("name"))
                objTable.Rows.Add(new object[] {el.Attribute("class").Value, el.Attribute("link").Value, el.Value });
            objTable.AcceptChanges();
    
            foreach (DataRow objRow in objTable.Rows)
            {
                Console.WriteLine(" {0} {1} {2} ", objRow["Type"], objRow["link"], objRow["filename"]);                
            }

    • Marked as answer by viggieuk Tuesday, January 28, 2014 1:20 PM
    Thursday, January 23, 2014 2:22 PM

All replies

  • You could make a second step and query z:

     var theRows = from n in z.Elements("name")
                              select new
                              {
                                  Type = n.Attribute("class").Value,
                                  link = n.Attribute("link").Value,
                                  filename = n.Value
                              };

    Below is code I used to test. Your sample xml is malformed, so I took a couple of liberties but you can see how it works (run it live here: http://dotnetfiddle.net/o2tYHl)

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string nodes = @"<Root><name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=1>Asbestos""> document A 21/01/2014</name>
     <name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=2Asbestos"">Asbestos Image 1 21/01/2014</name>
     <name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=3Asbestos"">Asbestos document B 21/01/2014</name></Root>";
    
                XDocument doc = XDocument.Parse(nodes);
    
                var theRows = from n in doc.Descendants("name")
                              select new
                              {
                                  Type = n.Attribute("class").Value,
                                  link = n.Attribute("link").Value,
                                  filename = n.Value
                              };
    
                foreach (var item in theRows)
                {
                    Console.WriteLine("Type: {0}, link: {1}, filename: {2}", item.Type, item.link, item.filename);
                }
            }
        }
    }
    


    Bob - www.crowcoder.com

    • Proposed as answer by Venkat786 Thursday, January 23, 2014 2:21 PM
    Thursday, January 23, 2014 1:47 PM
  • +1 Vote for Molku..

    Here is enhancement to get DataTable as output. Hope this helps as start..

     string nodes = @"<Root><name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=1>Asbestos""> document A 21/01/2014</name>
     <name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=2Asbestos"">Asbestos Image 1 21/01/2014</name>
     <name class=""Document"" icon=""Images/Icons/Document.png"" link=""http://site.com?docID=3Asbestos"">Asbestos document B 21/01/2014</name></Root>";
    
            DataTable objTable = new DataTable();
            objTable.Columns.Add("Type", typeof(string)); objTable.Columns.Add("link", typeof(string)); 
            objTable.Columns.Add("filename", typeof(string));
           
            XDocument objDoc = XDocument.Parse(nodes); // XDocument.load("your xml file");
            foreach (var el in objDoc.Descendants("name"))
                objTable.Rows.Add(new object[] {el.Attribute("class").Value, el.Attribute("link").Value, el.Value });
            objTable.AcceptChanges();
    
            foreach (DataRow objRow in objTable.Rows)
            {
                Console.WriteLine(" {0} {1} {2} ", objRow["Type"], objRow["link"], objRow["filename"]);                
            }

    • Marked as answer by viggieuk Tuesday, January 28, 2014 1:20 PM
    Thursday, January 23, 2014 2:22 PM
  • Thanks for the help. Unfortunately I don't have control over the XML as it comes from an external system.
    Tuesday, January 28, 2014 1:22 PM
  • That's great, just what I was trying to do. Thanks.
    Tuesday, January 28, 2014 1:22 PM