将LINQ转换为XML

3
<?xml version="1.0" encoding="UTF-8" ?>
<Accounts>
  <Account id="usama" password="3210" lastUpdated="6/16/2011 11:21:59 AM" nextUpdate="6/16/2011 11:36:59 AM">
    <SubAccount id="false">
      <Url>yahoo</Url>
      <Review>not so good</Review>
    </SubAccount>
    <SubAccount id="false">
      <Url>google</Url>
      <Review>as good as heard.</Review>
    </SubAccount>
  </Account>
</Accounts>

假设我想获取所有最后更新日期小于或等于今天(假设为2011年6月17日)的结果。

因此,我的结果应该像这样。

Accout id =usama ,passwod =3210 ,url=yahoo, review=not so good
Accout id =usama ,passwod =3210 ,url=google, review=as good as heard

我已经写了查询语句。
var q = from c in doc.Descendants("Accounts")
        from a in c.Descendants("Account")
        where a.Attribute("nextUpdate").Value == "6/16/2011 11:36:59 AM"
        select new
               {                        
                 accountName = a.Attribute("id").Value,                       
                 password = a.Attribute("password").Value,                        
                 url = a.Descendants("SubAccount").Descendants("Url").ToString() 
                 //review=a.Attribute("nextUpdate").Value
               }

我已经成功获取了用户名和密码,但是我不知道如何获取URL和评论。还有,如何将Attribute("nextUpdate")转换为日期时间以便我可以将其与日期进行比较?

3个回答

3

尝试这个(已经测试并且有效):

DateTime someTime = DateTime.Now;
var q = from a in doc.Descendants("Account")
        from sub in a.Elements("SubAccount")
        where (DateTime)a.Attribute("nextUpdate") <= someTime 
        select new
        {  
            accountName = a.Attribute("id").Value,  
            password = a.Attribute("password").Value,  
            url = (string)sub.Element("Url").Value,
            review = (string)sub.Element("Review").Value
        }

这里使用了Linq to XML提供的DateTime转换(也可参见此处)。


1

像这样的东西?

var q =
    from account in doc.Descendants("Account")
    from subAcount in account.Elements("SubAccount")
    where DateTime.Parse(account.Attribute("nextUpdate").Value) == filterDateTime
    select new
    {
        accountName = account.Attribute("id").Value,
        password = account.Attribute("password").Value,
        url = subAcount.Element("Url").Value,
        review = subAcount.Element("Review").Value
    };

filterDateTime 是一个 DateTime 类型的变量。


这将仅返回帐户中第一个“SubAccount”元素的结果,而不是OP想要的。 - BrokenGlass
谢谢!但是这个查询语句只会返回节点的第一个元素。所以它返回的是账户ID为usama,密码为3210,网址为yahoo,评价为不太好。我也想要第二个节点,就像问题中所问的那样。 - Zain Ali
@Zain,@BrokenGlass - 是的,我忽略了 XML 中的多个子帐户。已经修复。 - Alex Aza

1

这是我的建议。

var doc = XElement.Load(...);
var q =
    from account in doc.Elements("Account")
    let nextUpdate = XmlConvert.ToDateTime(account.Attribute("nextUpdate").Value, "your date format").Date
    where nextUpdate <= DateTime.Today
    from subAccount in account.Elements("SubAccount")
    select new
    { 
      Id = account.Attribute("id").Value,
      Password = account.Attribute("password").Value,
      Url = subAccount.Element("Url").Value,
      Review= subAccount.Element("Review").Value,
    }

别忘了用适当的格式字符串替换“你的日期格式”。 http://msdn.microsoft.com/en-us/library/kzk5c6y9.aspx


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接