根据条件从XML中删除元素 [英] Remove element from XML based on condition

查看:73
本文介绍了根据条件从XML中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





以下是我的XML结构。



XML

Hi,

Below is the XML structure I have.

XML

<Info>
<Details>
<ID>user</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>22-10-2016</EndDate>
</Details>
<Details>
<ID>user1</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>24-10-2016</EndDate>
</Details>
<Details>
<ID>user2</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>22-11-2016</EndDate>
</Details>
</Info>







Now I need to compare the EndDate node with today's date. If EndDate is lesser than today's date then I need to remove the whole contents of Details. For Ex: Let's assume strDate a string has today's date.

The below code takes all the values of EndDate and now how can I compare the string value and loop through xml elements and delete them.










Now this whole portion should be deleted as of comparing with todays date.




<Details>
<ID>user</ID>
<StartDate>23-10-2016</StartDate>
<EndDate>22-10-2016</EndDate>
</Details>





真的很感激任何建议。



我尝试过:



object [] arr = xDos.Descendants(Details)。Elements(EndDate)。

Select(element = element.Value ).ToArray();



Really appreciate any suggestions on this.

What I have tried:

object[] arr = xDos.Descendants("Details").Elements("EndDate").
Select(element = element.Value).ToArray();

推荐答案

如果XML不包含EndDate节点或者EndDate节点不是有效日期,此解决方案将起作用但可能会失败(我已将其创建为单元测试)[这可能是通过某种形式的XSD验证来缓解,其中EndDate是强制日期]



This solution will will work but could fail if the XML either does not contain the EndDate node or the EndDate node is not a valid Date (I have created it as a Unit Test) [This could be mitigated by some form of XSD validation of the XML where the EndDate is a Mandatory Date]

public void TestMethod1()
        {
            var todaysDate1 = new DateTime(2001, 10, 30); // no nodes selected
            var todaysDate2 = new DateTime(2016, 10, 28); // one node selected
            var todaysDate3 = new DateTime(2016, 10, 30); // 2 nodes selected
            var todaysDate4 = new DateTime(2016, 12, 25); // 3 nodes selected

            var xmlData ="<Info><Details><ID>user</ID><StartDate>23-10-2016</StartDate><EndDate>22-10-2016</EndDate></Details><Details><ID>user1</ID><StartDate>23-10-2016</StartDate><EndDate>29-10-2016</EndDate></Details><Details><ID>user2</ID><StartDate>23-10-2016</StartDate><EndDate>22-11-2016</EndDate></Details></Info>";
            XDocument xdoc = XDocument.Parse(xmlData);

            var nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate1) > 0).Remove();

            Assert.AreEqual(0, xdoc.Descendants("Details").Count());

            // Test 2
            xdoc = XDocument.Parse(xmlData);
            nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate2) > 0).Remove();
            Assert.AreEqual(1, xdoc.Descendants("Details").Count());

            // Test 3
            xdoc = XDocument.Parse(xmlData);
            nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate3) > 0).Remove();
            Assert.AreEqual(2, xdoc.Descendants("Details").Count());

            // Test 4
            xdoc = XDocument.Parse(xmlData);
            nodes = xdoc.Descendants("Details");
            nodes.Where(x => DateTime.Compare(DateTime.Parse(x.Element("EndDate").Value), todaysDate4) > 0).Remove();
            Assert.AreEqual(3, xdoc.Descendants("Details").Count());
        }


Null异常的问题困扰着我 - 但是感谢Resharper这里有另一种选择(我无法解决这个问题)日期问题)



The issue of the Null exception bothered me - but thanks to Resharper here is an alternative (I have not been able to resolved the invalid date issue)

nodes.Where(x =>
                {
                    var element = x.Element("EndDate");
                    return element != null && DateTime.Compare(DateTime.Parse(element.Value), todaysDate1) > 0;
                }).Remove();


这篇关于根据条件从XML中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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