0

サンプル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>
4

2 に答える 2

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

私はこの助けを願っています

于 2012-07-26T08:11:23.413 に答える
0

selectで何をしようとしているのかよくわかりませんが、属性の値を変更する場合は、次のようにします。

        XAttribute attr = doc.Descendants(ns + "SiEventSchedule").Select(x => x.Attribute("deleteStart")).First();
        attr.SetValue(attr.Value.Replace(oldValue, newValue));
于 2012-07-26T08:11:23.580 に答える