我需要什么 Linq to XML 将 XML 绑定到 Silverlight Datagrid

发布于 2024-09-29 04:44:08 字数 1206 浏览 5 评论 0原文

我正在查看这个问题和它给了我大约 80% 的需要。对我来说,“问题”是我的 XML 结构不同,我不太确定如何使用 Linq 来获得我需要的东西。

我的 XML 看起来像这样(顺便说一句,它是由 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>

我试图绑定到 Silverlight DataGrid,以便列名称为“Name”和“LastWriteTime”,并且行将具有值那是在属性标签中。对于第一个,名称为 Compiled Code.ps1 ,LastWriteTime 为“5/21/2009 6:59:16 PM”

上述问题的答案采用了此方法

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;
        }

我创建了自己的名为 Scripts 的类,它有一个名称和日期,但我试图弄清楚需要哪些元素或属性来填充数据网格。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2024-10-06 04:44:08

这是您需要的基本要点:-

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);
 }

您可能希望 LastWriteTime 成为 DateTime 因此您可能希望将 is 放在中间字符串中并使用精确解析。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文