Linq foreach to Gridview

dingir
dingir used Ask the Experts™
on
This must be easy but i'm a newbie so far..

I have some results from a linq..

   var sa = from n in db.Users
                 where n.uid == usr
                 orderby n.User descending
                 select n;

I now want this result to an xml output (this is a webservice).
How do I do? whatever I did, this xml will give me only one hit.
If this was a plain aspx file i was using;
        GridView1.DataSource = report;
        GridView1.DataBind();

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi dingir;

What is the layout that you want the XML file to be in?
In your Linq query above does the result contain 1 or does it contain many values?

Fernando

Author

Commented:
The result of the linq above is giving me results like

userid       date                      nr       somethingY
nike           2009-01-01          3             hey
reload        2009-01-01          3             ho
mummy      2009-01-01           2            obey
....


I've done a
                             select new
                             {
                               su = n.su,
                               username = n.Username,
                               vip = n.vip,            
                               last = n.last,            
                             }) as List<DataTable>;

but i'm stucked! The only samples I find on net is by code manually added text lists..

Author

Commented:
May probably also have something to do with the header of the class (because of the web service)

    [WebMethod(Description = "")]
    public List<DataTable> ListP()
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Fernando SotoRetired
Distinguished Expert 2017

Commented:
You say you want the result to be in XML format, so how do you want the XML to look like?

For example this is the format of a XML file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Properties.Settings.NORTHWNDConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=NORTHWND.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Open in new window

Author

Commented:
Well the result to xml it by design.. I call the webservice through webb..
http://localhost:1300/xy/WebService/xy.asmx?op=ListP and got the result in xml..

I need the output to fill a list on an aspx page through ajax/json.
Retired
Distinguished Expert 2017
Commented:
Hi dingir;

Not sure what you want but try this query which will return XML data.

Fernando
using System.Xml;
using System.Xml.Linq;
 
 
var sa = from n in db.Users
         where n.uid == usr
         orderby n.User descending
         select new
         {
             User = new XElement("User",
                        new XElement("su", n.su),
                        new XElement("username", n.username),
                        new XElement("vip", n.vip),
                        new XElement("last", n.last))
         };
 
XElement xmlData = new XElement("Root");
foreach(var x in sa)
{
    xmlData.Add(x.User);
}
 
Console.WriteLine(xmlData);

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial