LINQ TO XML, How to replace values with new values c#

后端 未结 2 1150
温柔的废话
温柔的废话 2021-01-15 01:38

Below is my Sample XML File:-

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



        
2条回答
  •  不思量自难忘°
    2021-01-15 02:03

    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);
        }
    

提交回复
热议问题