我有一個在.net 2.0上的應用程序,我對它有一些困難,因爲我更習慣於linq。使用.net2.0計算元素和讀取屬性?
的XML文件是這樣的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<updates>
<files>
<file url="files/filename.ext" checksum="06B9EEA618EEFF53D0E9B97C33C4D3DE3492E086" folder="bin" system="0" size="40448" />
<file url="files/filename.ext" checksum="CA8078D1FDCBD589D3769D293014154B8854D6A9" folder="" system="0" size="216" />
<file url="files/filename.ext" checksum="CA8078D1FDCBD589D3769D293014154B8854D6A9" folder="" system="0" size="216" />
</files>
</updates>
文件被下載並readed上飛:
XmlDocument readXML = new XmlDocument();
readXML.LoadXml(xmlData);
起初,我以爲它會像這樣的東西去:
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//files");
foreach (XmlNode node in nodes)
{
... im reading it ...
}
但在閱讀它們之前,我需要知道它們在我的進度條上有多少使用,而我是als o在這種情況下,如何獲取文件元素的屬性無能爲力。
- 我怎麼能算多少「文件」 ELEMENTS我有(進入的foreach OFC之前指望他們)和閱讀他們的 屬性?
我需要計數,因爲它將用於更新進度條。
總的來說,它並沒有很好地閱讀我的xml。
我真的很感謝你的回覆和時間,它涵蓋了我所有的疑惑,並且還有更多的簡單易懂的例子和指向一些有趣的文檔,你甚至花時間表明我可以使用int.Parse或int.TryParse,謝謝非常。 – Prix 2011-03-02 05:59:37
@Prix:沒問題。你也可以看看Xml序列化來做到這一點,這是我建議的解決方案,但這更加複雜:) – 2011-03-02 23:16:45
我實際上正在研究它,使它工作是第一步嘿,我其實是想要序列化它從一開始,但由於我做得不好,我來請求一些備份ehhehe;) – Prix 2011-03-03 00:16:42