1
我正在開發一個應用程序,它保存並從XML讀取首選項。當我添加新功能時,我當然也添加了配置這些功能的功能,但是當我嘗試從XML中讀取尚不存在的變量時,我發現我的代碼崩潰。發生這種情況時,應讀取的任何後續變量都不會被讀取,這使我失去了一些偏好。考慮以下代碼爲例:德爾福XML解析和EVariantTypeCastError
xmldoc := TXMLDocument.Create(nil);
Try
xmldoc.LoadFromFile('c:\myxml.xml');
xmldoc.Active := True;
if xmldoc.ChildNodes.Count >= 1 then
Begin
Control := Root.ChildNodes.FindNode('Control');
if Assigned(Control) then
Begin
Username.Text := Control.Attributes['Username'];
Password.Text := Control.Attributes['Password'];
AutoValidate.Checked := Control.Attributes['AutoValidate'];
AutoIRC.Checked := Control.Attributes['AutoIRC'];
Passive.Checked := Control.Attributes['Passive'];
DoNothing.Checked := Control.Attributes['DoNothing'];
OpenPage.Checked := Control.Attributes['OpenPage'];
DownloadUsingBrowser.Checked := Control.Attributes['DownloadUsingBrowser'];
RSSFeed.Checked := Control.Attributes['RSSFeed'];
SaveToFolder.Checked := Control.Attributes['SaveToFolder'];
SaveToFTP.Checked := Control.Attributes['SaveToFTP'];
SavePath.Text := Control.Attributes['SavePath'];
FTPPath.Text := Control.Attributes['FTPPath'];
End;
End;
Finally
xmldoc.Active := False;
End;
承擔,我剛剛加入「AutoValidate」作爲一個選項時刻 - XML文件沒有這個屬性,因爲它尚未保存應用程序,但應用程序在啓動時仍會嘗試讀取此屬性(讀取首選項)。
我怎樣才能得到它,這樣缺少的屬性不提高好老「Exception類EVariantTypeCastError與消息‘類型的變種(空)無法轉換爲類型(布爾)’」
非常感謝 - 我想編輯您的答案,因爲你拼寫爲「ADefualt」錯誤的第一行。但編輯必須至少有6個字符!也許你可以編輯? – KeyszerS
@ keyszers:好的,完成了。 – pf1957
非常感謝,從現在開始,我的應用程序將變得更加穩定:D – KeyszerS