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>
    </SubAccount>
    <SubAccount id="false">
      <Url>google</Url>
      <Review>as good as heard.</Review>
    </SubAccount>
  </Account>
</Accounts>

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

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 wrote a request 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 get the username and password in order, but I don't know how to get the url and view. Also, how to discard the attribute ("nextUpdate") in the place where there is time to date so that I can compare it with the date?

+3
source share
3 answers

Try this (tested and working):

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 cast DateTimeprovided by Linq to XML (see here ).

+3
source

Something like that?

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

filterDateTimeis a variable DateTime.

+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

+1

All Articles