开发者

Format XML data to display on a gridview

I am trying to format XML data to display on a grid.

Page1.aspx. This inserts XML data stored a xmldatatype:

WorkHistory workhis = js.Deserialize<WorkHistory>(json);
XmlDocument work = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "root");                
objBLL.insert_XMLWork(work, Convert.ToInt64(ui.id));

Page2.aspx retrieves it and display on a grid:

DataTable FBWorkDt = objBLL.get_FacebookWork(FacebookUserId);
GrdWork.DataSource = FBWorkDt;
GrdWorkPub.DataBind();        
get_FacebookWork(select workinfo from Fprofiles where Userid = FacebookUserId)

returns a DataTable

It displays in this format exactly.

WorkInfo
<root><work><employer><id>208571635850052</id><name>Netizen Apps</name></employer></work><开发者_如何学Goid>1076483621</id></root>

How do I make a normal display instead of XML format?

Thanks Sun


It depends a good deal on the shape of the DataTable you're returning, but assuming you want the display to be something like this:

`ID Name

-------------------- ---------------------

208571635850052 Netizen Apps`

You could use LINQ:

DataTable FBWorkDt = objBLL.get_FacebookWork(FacebookUserId);
var query = from x in FBWorkDt.AsEnumerable()
            select new {
                    id = x.ID,
                    name = x.Name
            };
GrdWork.DataSource = query.ToList();
GrdWorkPub.DataBind(); 

I haven't tried the code out, so there may be minor syntatic changes, but essentially what it's doing is:

  1. Use LINQ to get a collection of a new anonymous type that has one entry per row with the id and name from the table. You have to use AsEnumerable() [contained in System.Data.DataSetExtensions].
  2. Convert the LINQ result set to a List via .ToList() and bind it to the GridView.

If you can post a little more information - what exactly you mean by display, and the expected shape of the returned DataTable (i.e., what the columns in each row are) we can give you a better answer.

UPDATE If you're storing the XML document above in your datastore and that is being returned in the table, try this code:

DataTable FBWorkDt = objBLL.get_FacebookWork(FacebookUserId);
XDocument xDoc = XDocument.Load(FBWorkDt.Rows[0][0].ToString());

var query = from x in xDoc.Descendants("employer")
            select new 
            {
                id = (string)x.Element("id"),
                name = (string)x.Element("name")
            }
GrdWork.DataSource = query.ToList();
GrdWorkPub.DataBind(); 

Same basic principal as above, except this time your querying over an XDocument instead of a DataTable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜