views:

18

answers:

1

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 Status();
            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.

+1  A: 

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.

AnthonyWJones