2

Below is my Sample XML File:-

I just want to replace date values with current date using LINQ to XML in C#.

<?xml version="1.0" encoding="UTF-8"?>
<BasicImport xmlns="http://www.uk.SSp.com/SSR/XTI/Traffic/0010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.uk.nds.com/SSR/XTI/Traffic/0010 0010.xsd" utcOffset="+05:30" frameRate="25">
  <SiEventSchedule deleteStart="2012/01/21 00:00:00" deleteEnd="2012/01/21 23:59:59">
    <siService>Ssp</siService>
    <playoutSource>Ssp</playoutSource>
    <activationSourceId>0</activationSourceId>
    <CaSchedule deleteStart="2012/01/21 00:00:00" deleteEnd="2012/01/21 23:59:59" />
    <SiEvent>
      <displayDateTime>2012/01/21 00:00:00</displayDateTime>
      <activationDateTime>2012/01/21 00:00:00</activationDateTime>
      <displayDuration>00:30:00</displayDuration>
      <siTrafficKey>056049263</siTrafficKey>
      <detailKey>056049263 2012-07-12</detailKey>
     </SiEvent>
   </SiEventSchedule>
</BasicImport>
1
  • I would be more inclined to use xpath instead of Linq to XML here. Commented Jul 24, 2012 at 19:32

2 Answers 2

5

Which date values? All of them? Specific elements? For example, this will replace all displayDateTime elements with the current date - in standard XML format, which isn't what your source XML contains... if you want a different format, you should use DateTime.ToString and replace the contents of the elements with the relevant text.

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

class Test
{
    static void Main()
    {
        XNamespace ns = "http://www.uk.ssp.com/SSR/XTI/Traffic/0010";
        XDocument doc = XDocument.Load("ssp.xml");

        var elements = doc.Descendants(ns + "displayDateTime")
                          .ToList();

        var today = DateTime.Today;
        foreach (var element in elements)
        {
            element.ReplaceAll(today);
        }
        Console.WriteLine(doc);
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Then you should find all the element names which would contain date/time values, and you should determine exactly what the date/time format you want is. For example: string text = DateTime.Today.ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture);
Thank you very much jon.. every thing is working good now.... But only one query i did not wanted to replace time value .. i only wanted to replace Date value.. :-) Can you please suggest me a way forward..
@Saman: You'll need to parse the existing value, and add the time of day to DateTime.Today.
Can somebody please let me know how can i change date value with current date on just above posted xml file using c#
@Saman: Well how far did you go with parsing the existing values? I've given you the format string you need, but I'm not going to spoon-feed you the complete code. Break the problem down into smaller problems: extracting existing values, parsing them, combining them with today's date, reformatting them.
|
2

You could do on following manner

    [Test]
    public void Test()
    {
        XElement root = XElement.Load("Data.xml");
        root.Descendants()
           .Where(x => x.Name.LocalName == "displayDateTime")
           .ToList()
           .ForEach(x => x.ReplaceNodes(GetDate(x)));
    }

    private static DateTime GetDate(XElement element)
    {
         return DateTime.Today.Add(DateTime.Parse(element.Value).TimeOfDay);
    }

Comments

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.