示例XML文件
<SiEventSchedule deleteStart="2012/01/21 00:00:00" deleteEnd="2012/01/21 23:59:59">
</SiEventSchedule>
代码:
var el = doc.Descendants(ns + "SiEventSchedule").Select(x => x.Attribute("deleteStart").Value).First();
string[] s = el.ToString().Split(' ');
s[0] = today.ToString("yyyy/MM/dd");
String s5 = String.Join(" ", s);
el.Replace(el, s5); // This line is not working
只有代码的最后一行不起作用,其他一切都正常。 任何人都可以建议如何更新此值?
<SiEventSchedule deleteStart="2012/01/21 00:00:00" deleteEnd="2012/01/21 23:59:59">
</SiEventSchedule>
答案 0 :(得分:0)
using System.Xml.Linq;
XDocument xmlFile = XDocument.Load("books.xml");
var query = from c in xmlFile.Elements("catalog").Elements("book")
select c;
foreach (XElement book in query)
{
book.Attribute("attr1").Value = "MyNewValue";
}
xmlFile.Save("books.xml");
我希望这个帮助
答案 1 :(得分:0)
不确定您正在尝试使用select进行什么操作,但如果您想更改属性的值,请按以下步骤操作:
XAttribute attr = doc.Descendants(ns + "SiEventSchedule").Select(x => x.Attribute("deleteStart")).First();
attr.SetValue(attr.Value.Replace(oldValue, newValue));