根据节点选择xml后代 [英] select an xml descendants based on a node

查看:67
本文介绍了根据节点选择xml后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML,我想根据日期选择一个元素.因此,日期后代的日期列表比今天少.

I have an XML and I want to pick an element based on a date. So Date descendants has list of dates less than today.

XML具有这样的值

<日期>
  <部门>
    < ID>食物</ID>
    < Date> 25-11-2016</Date>
  </部门>
  <部门>
    < ID>运动</ID>
    < Date> 26-10-2016</Date>
  </部门>
</日期>

<Dates>
  <Department>
    <ID>Food</ID>
    <Date>25-11-2016</Date>
  </Department>
  <Department>
    <ID>Sport</ID>
    <Date>26-10-2016</Date>
  </Department>
</Dates>

现在,我只需要选择日期小于今天的元素,下面的查询将选择日期并将其从xml中删除.

Now I need to pick only the elements where the respective date is less than today and the below query will pick the dates and remove the items from xml.

XDocument newXML = XDocument.Load(new StringReader(xmlValues));
var q =来自newXML.Descendants("ExpiryDate")中的节点.
       让attr = node.Value其中attr!= null&& DateTime.ParseExact(attr,"dd-MM-yyyy",CultureInfo.InvariantCulture)< DateTime.Today选择节点.Parent;           
q.ToList().ForEach(x => x.Remove());

XDocument newXML = XDocument.Load(new StringReader(xmlValues));
var q = from node in newXML.Descendants("ExpiryDate")          
         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());

现在我该如何只选择日期比今天短的以下物品?

Now how can I pick only the below items which has date less than today?

<部门>
    < ID>运动</ID>
    < Date> 26-10-2016</Date>
</部门]

<Department>
    <ID>Sport</ID>
    <Date>26-10-2016</Date>
</Department>



推荐答案

您好vickyt0,

Hi vickyt0,

谢谢您在这里发布.

对于您的问题,您可以尝试以下代码.该程序将删除数据晚于 今天保存的数据少于今天.

For your question, you could try the following code. The program will remove the data later than today and save the data less than today.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace pick_xml
{
    class Program
    {
        public class Building
        {
            public string BuildingName { get; set; }
        }
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(@"C:\Users\v-wezan\Desktop\N1.xml");

            string tody = DateTime.Now.ToShortDateString();
            XElement root = doc.Element("Dates");
            var list = doc.Descendants("Department");

            //var result = list.

            foreach (var item in list)
            {
                if (DateTime.Compare(DateTime.ParseExact(item.Element("Date").Value, "dd-MM-yyyy", CultureInfo.InvariantCulture), DateTime.Today) > 0)
                {
                    item.RemoveAll();
                    doc.Save(@"C:\Users\v-wezan\Desktop\N2.xml");
                }
            }

        }
    }
}

我希望这会对您有所帮助.

I hope this would be helpful to you.

如果还有其他问题,请随时与我们联系.

If you have something else, please feel free to contact us.

最好的问候,

温迪


这篇关于根据节点选择xml后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆