我在寫一個函數來更新我的XML,並遇到一些問題。我希望我可以直接將舊的XElement設置爲更新的XElement,但這在foreach循環中不起作用(儘管應該只是一個查詢結果)。我打算對每個字段進行硬編碼,但後來決定使用循環。然而,當它擊中的部分,它的子元素它讀取它作爲一個單一的元素,攪亂了文件在複雜的XML中更新數據
var myDevice = from c in appDataXml.Descendants("device")
where (string)c.Element("name") == App.CurrentDeviceName
&& (string)c.Element("ip") == App.CurrentIp
select c;
if (myDevice.Any())
{
foreach (XElement device in myDevice)
{
//device.Element("customname").Value = updatedDevice.Element("customname").Value;
for (int a = 0; a < device.Elements().Count(); a++)
{
string field = device.Elements().ElementAt(a).Name.ToString();
device.Element(field).Value = updatedDevice.Element(field).Value;
}
}
}
示例XML
<Devices>
<device>
<name>blah</name>
<customname>custom</customname>
<ip>192.168.1.100</ip>
<port>1000</port>
<sources>
<source>
<code>00</code>
<name>blah2</name>
<hidden>False</hidden>
</source>
<source>
...etc
</sources>
</device>
這看起來很重要,這是一個Windows Phone 7項目 - 擴展器使用的TypeDescriptor看起來並沒有實現 – Jason 2012-03-08 22:42:16
您可以嘗試創建自己的:http:// msdn.microsoft.com/en-us/library/ayybcxe5.aspx – 2012-03-08 22:52:59
或者,您可以創建使用GetString()例如int,GetInt()的GetInt(),GetBool() {return Convert.ToInt32(GetString(...)); }' – 2012-03-08 22:55:09