开发者

What Linq to XML do I need to bind XML to Silverlight Datagrid

I was looking at this question and it gave me about 80% of what I needed. The "problem" for me is that my XML is strucutred differently and I am not quite sure how I would go about using Linq to get at what I need.

My XML looks like this (By the way, its generated by the ConvertTo-XMl PowerShell Cmdlet)

<?xml version="1.0"?>
<Objects>
  <Object>
    <Property Name="Name">CompiledCode.ps1</Property>
    <Property Name="LastWriteTime">5/21/2009 6:59:16 PM</Property>
  </Object>
  <Object>
    <Property Name="Name">ComputerDrawing.ps1</Property>
    <Property Name="LastWriteTime">1/13/2010 7:52:44 AM</Property>
  </Object>
</Objects>

I am trying to bind to a Silverlight DataGrid so that the column names will be "Name" and "LastWriteTime" and the rows will have the vale that is in the Property Tag. For the first one, the name would be Compiled Code.ps1 and LastWriteTime would be "5/21/2009 6:59:16 PM"

The answer to the above mentioned question had this method

private Status GetStatus(XElement el)
        {
            Status s = new S开发者_Python百科tatus();
            s.Description = el.Attribute("Description").Value;
            s.Date = DateTime.Parse(el.Attribute("Date").Value);
            return s;
        }

I created my own class called Scripts which has a Name and a Date, but I am trying to figure out what elements or attributes I need to populate my datagrid.


This is the basic gist of what you need:-

private Scripts GetScripts(XElement el)
{
    Scripts s = new Scripts()

    s.Name = (string)el.Elements("Property")
        .Where(e => (string)e.Attribute("Name") == "Name")
        .FirstOrDefault();

    string lastWriteTime = (string)el.Elements("Property")
        .Where(e => (string)e.Attribute("Name") == "LastWriteTime")
        .FirstOrDefault();

    s.LastWriteTime = DateTime.ParseExact(lastWriteTime, "M/d/yyyy h:m:s tt", CultureInfo.InvariantCulture);
 }

You would probably want LastWriteTime to be a DateTime so you might want to place is in an intermediatory string and use a parse exact.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜