开发者

Remove items from xml with specific date through linq to xml in C#

I have xml file that looks like this..

<RecentInformation>
  <User Id="99">
    <Customers>
      <Customer>
        <CustomerId>7</CustomerId>
        <Date>1/20/2010</Date>
      </Customer>
      <Customer>
        <CustomerId>67</CustomerId>
        <Date>1/20/2010</Date>
      </Customer>
    </Customers>
    <Suppliers>
      <Supplier>
        <SupplierId>11619</SupplierId>
        <Date>1/20/2010</Date>
      </Supplier>
      <Supplier>
        <SupplierId>16274</SupplierId>
        <Date>1/20/2011</Date>
      </Supplier>
    </Suppliers>
    <Enquiries>
      <Enquiry>
        <EnquiryId>136202</EnquiryId>
        <Date>1/20/2010</Date>
      </Enquiry>
      <Enquiry>
        <EnquiryId>134507</EnquiryId>
        <Date>1/20/2011</Date>
      </Enquiry>
    </Enquiries>
    <Quotes>
      <Quote>
        <QuoteId>34008</QuoteId>
        <Date>1/20/2010</Date>
      </Quote>
      <Quote>
        <QuoteId>37356</QuoteId>
        <Date>1/20/2011</Date>
      </Quote>
    </Quotes>
    <Bookings>
      <Booking>
        <BookingId>3070</BookingId>
        <Date>1/20/2011</Date>
      </Booking>
      <Booking>
        <BookingId>3251</BookingId>
        <Date>1/20/2011</Date>
      </Booking>
    </Bookings>
  </User>
</RecentInformation>

I want to remove all开发者_如何学Go the nodes that should be less then say 15 days old.

I am using this code but it is not working for me..

xdoc.Descendants("User").Descendants().Descendants().Where(x => DateTime.Parse(x.Element("Date").Value) < date)
                            .Remove();

any idea??


This is your line fixed

xdoc.Descendants("Date").Where(x => DateTime.Parse(x.Value) < date).Select(x=>x.Parent).Remove();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜