在XML 檔案中若節點中有Attribute 的值,即可利用此值作為搜尋條件,找到此值後把Element 內的值讀取出來
RuleBase.xml 的檔案內容
<?xml version="1.0" encoding="utf-8"?>
<RuleBase>
<Simulation>
<Process ID="Mail">
<TimeOutCount>301</TimeOutCount>
<TimeLimit>600</TimeLimit>
</Process>
</Simulation>
</RuleBase>
using System.Xml.Linq;
try // 讀取XML 設定檔中的參數
{
XElement root = XElement.Load("RuleBase.xml");
IEnumerable<XElement> tests =
from el in root.Elements("Simulation").Elements("Process")
where (string)el.Attribute("ID") == "Mail"
select el;
foreach (XElement el in tests)
{
MessageBox.Show(el.Attribute("ID").ToString() + " : " + el.Element("TimeOutCount").Value); //查詢xml Element 的值
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}