看板 C_Sharp 關於我們 聯絡資訊
前文述刪 ※ 引述《chhuang (瓶水相逢)》之銘言: : 程式碼: : var xd = XDocument.Load(@"XMLFile.xml"); : var q = from x in xd.Descendants("test") : where (int)x.Attribute("mid") == 1 && (bool)x.Attribute("enble") : select x.Element("modle").Value; : foreach (var o in q) : { : MessageBox.Show(o); : } 無意戰 但像這種問題我建議還是用 XmlDocument 來解決就好了 一旦用到了 XDocument, linq 等等,就讓程式的可攜性下降了 以目前 .Net Framework 3.5 還不普及的情況下 Console 或 Window AP 的程式都還是用 2.0 就好了 順便提一下上面的寫法在 2.0 用 XmlDocument 來實作的方法: XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"XMLFile.xml"); XmlNodeList nodes = xmlDoc.SelectNodes("/root/test[@mid=1 and @enble=TRUE]/modle"); foreach(XmlElement element in nodes) { MessageBox.Show(element.InnerText); } -- my blog: http://kenshinnn.blogspot.com operation system : Ubuntu GNU/linux window manager : Gnome -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.117.165.212
chhuang:總要有人用才會普及...才會有人跟進...:p 09/07 00:43
icant:但如果沒有需要或益處 何必把需求提高呢? :p 09/07 00:56
icant:另 比起直接下XPath linq那段code似乎造成了額外的成本 09/07 00:59
icant:還是感謝1樓大方分享作法 ^^ 09/07 01:00