2

I need help on how to "automatically" delete a node based on timestamp. A particular date is defined by the user inside a xml document e.g. 17/9/2006 Can someone provide me an example? Thanks in advance!

   <root>
    <element>
    </element>
    <timestamp time="2016-09-16T13:45:30">
    </timestamp>
    <--how do I delete element based on the given timestamp?-->
    </root>

  //UNTESTED CODE

     XDocument doc = XDocument.Load("time.xml");
     var name = doc.Descendants("root")
        .Where(n => n.Attribute("time").Value == "2016-09-16T13:45:30")
        .Select(n => (string)n) 
        .First(); 
      <--how can I delete it based on timestamp-->
         name.Element("element").Remove();
8
  • This is no valid xml. the timestamp node should have an attribute whose value is your actual timestamp. Commented Sep 15, 2016 at 9:59
  • You mean the standard datetime format, correct, but the above is just a demo xml. Commented Sep 15, 2016 at 10:02
  • Since your xml is now valid, what is your exact problem ? Parsing the xml and identifying the node, or parsing the ISO date format ? Commented Sep 15, 2016 at 10:06
  • I don't know how to use timestamp function. I'm familiar enough with LINQ, identifying the node isn't an issue. thanks. Commented Sep 15, 2016 at 10:11
  • Please share what have you tried? Commented Sep 15, 2016 at 10:14

2 Answers 2

4

Parsing ISO 8601 date/time format:

string input = "2016-09-16T13:45:30";
DateTime converted = DateTime.Parse(input, null, DateTimeStyles.RoundtripKind);

Once the date converted to a DateTime type, you can use it identify the node you wish to remove (and using LinQ for that is highly recommended).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. I've updated my code. So I will compare "converted" with time attribute?
When you parse your XML, you will convert the time attribute, which is of type string, to a DateTime with the code snippet I provided, and compare this DateTime to the one you use to decide whether the node should be deleted or not.
3

Lets suppose you want to compare with a DateTime variable inputDate.

// I have formatted yor XML and structured it. "root" is the the parent node. Elements are the child elements of root consisting of timestamp tag.

 string xmlInput =  @"
 <root>
 <element>
 <timestamp time='2016-09-16T13:45:30'>
 </timestamp>
 </element>
 <element>
 <timestamp time='2016-10-16T13:45:30'>
 </timestamp>
 </element>
 </root>";

    XDocument  xdoc = XDocument.Parse(xmlInput);
    xdoc.Descendants("root").Elements("element").
                             Where(x => DateTime.Compare(DateTime.Parse(x.Element("timestamp").Attribute("time").Value,null, DateTimeStyles.RoundtripKind).Date, inputDate.Date) ==0).
                             ToList().ForEach(x => x.Remove());

I have compared the xml date timestamp for each element with inputdate for equality of only Date not the time. You can have any condition you want.

Note: You need to refer using System.Globalization;

using System.Globalization;
using System.Xml.Linq;
using System.Xml;
using System.Linq;

1 Comment

Τhanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.