我想在XDocument的幫助下閱讀XML文檔。我已經通過互聯網搜索正確的解決方案。我發現這個example,但沒有像它應該的工作。xml xdocument,讀屬性
我的XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<blabla>
<Infos>
<ConfigurationName>XXConfigurationName</ConfigurationName>
<DateSaved>14.10.2015 10:41:54</DateSaved>
</Infos>
<Configuration>
<DayRoutine>
<DayRoutine ID="4">
<Button>1</Button>
<DayRoutinePoints>
<Point0>00:00:00, 243</Point0>
<Point1>00:00:00, 243</Point1>
<Point2>00:00:00, 243</Point2>
<Point3>00:00:00, 243</Point3>
</DayRoutinePoints>
</DayRoutine>
<DayRoutine ID="3">
<Button>5</Button>
<DayRoutinePoints>
<Point0>00:00:00, 243</Point0>
<Point1>00:00:00, 243</Point1>
<Point2>00:00:00, 243</Point2>
<Point3>00:00:00, 243</Point3>
</DayRoutinePoints>
</DayRoutine>
</DayRoutine>
</Configuration>
</blabla>
而我的C#代碼 - 更新:
XDocument doci = XDocument.Load(path);
var mijav = from r in doci.Descendants("Configuration").Descendants("DayRoutine").Descendants("DayRoutine").Where(r => (int)r.Attribute("ID") == 4)
select new
{
Button = r.Element("Button").Value,
DataPoints = r.Elements("DayRoutinePoints").Select(c => (string)c.Value).ToList(),
};
我目前的C#代碼不給我任何東西。我喜歡從DayRoutine ID =「3」讀取數據。怎麼做?因爲我試圖擦除「Where(r =>(int)r.Attribute(」ID「)== 3)」,並且我從第一個「DayRoutine」中得到結果。但我喜歡從第二個DayRoutine獲取數據。
後來我喜歡讀點。但是因爲它不是每次都有相同的點數,所以如何用循環來讀取這些點?
感謝您的幫助,並請詢問如果您有任何疑問
你是什麼意思「不要給我任何東西。」 ?代碼看起來很好,XML可以設計得更好(命名爲外部標籤''。 –
結果查看來回空,沒有任何內容。 – esispaned
添加'var result = mijav.FirstOrDefault()'並告訴我們什麼是在我看來這裏沒有直接的缺陷 –