开发者

Convert LINQ to XML

<?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&g开发者_如何学编程t;
    </SubAccount>
    <SubAccount id="false">
      <Url>google</Url>
      <Review>as good as heard.</Review>
    </SubAccount>
  </Account>
</Accounts>

Let suppose I want to get all those results whose last Updated date is less than or equal today(suppose 6/17/2011) date.

So my result should look like this.

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

I have written the query so far

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
               }

I am getting the user name and password fine but I do not know how to get URL and review. Also how to cast Attribute("nextUpdate") in where clause to date time so that I can compare it with date?


Try this (tested and works):

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
        }

This uses the DateTime cast provided by Linq to XML (also see here).


Here is my proposal.

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

Don't forget to replace "your date format" with an appropriate format string. http://msdn.microsoft.com/en-us/library/kzk5c6y9.aspx


Something like this?

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 is a variable of DateTime.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜