选择一个基于节点的xml后裔
我有一个XML,我想根据日期选择一个元素。所以日期后裔的日期列表比今天少。选择一个基于节点的xml后裔
XML有这样
<Dates> <Department>
<ID>Food</ID>
<Date>25-11-2016</Date>
</Department>
<Department>
<ID>Sport</ID>
<Date>26-10-2016</Date>
</Department>
</Dates>
值现在,我需要选择只有元素,其中各自的日期小于今天和下面的查询将选择日期,并从XML删除的项目。
XDocument newXML = XDocument.Load(new StringReader(xmlValues)); var q = from node in newXML.Descendants("Date")
let attr = node.Value where attr != null && DateTime.ParseExact
(attr, "dd-MM-yyyy", CultureInfo.InvariantCulture) < DateTime.Today
select node.Parent;
q.ToList().ForEach(x => x.Remove());
现在我该如何挑选只有日期少于今天的以下物品?
<Department> <ID>Sport</ID>
<Date>26-10-2016</Date>
</Department>
回答:
试试这个
string xml = "<Dates>" +
"<Department>" +
"<ID>Food</ID>" +
"<Date>25-11-2016</Date>" +
"</Department>" +
"<Department>" +
"<ID>Sport</ID>" +
"<Date>26-10-2016</Date>" +
"</Department>" +
"</Dates>";
XElement dates = XElement.Parse(xml);
List<XElement> departs = dates.Descendants("Department").Where(x => DateTime.ParseExact(x.Element("Date").Value, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture) < DateTime.Now).ToList();
以上是 选择一个基于节点的xml后裔 的全部内容, 来源链接: utcz.com/qa/259629.html