The XML is as followed:
XML如下:
<?xml version="1.0" encoding="utf-8"?>
<Folder_Settings>
<Documents>Checked</Documents>
<Pictures>Not Checked</Pictures>
<Music>Checked</Music>
<Videos>Not Checked</Videos>
<Downloads>Checked</Downloads>
<Contacts>Checked</Contacts>
<Favorites>Not Checked</Favorites>
<Other>Checked</Other>
<OtherFolderSettings>C:\Users\Asus\Desktop\revouninstaller-portable</OtherFolderSettings>
<OtherFolderSettings>D:\Personal Website</OtherFolderSettings>
<OtherFolderSettings>D:\testing</OtherFolderSettings>
<OtherFolderSettings>C:\Users\Asus\Desktop\revouninstaller-portable</OtherFolderSettings>
<OtherFolderSettings>C:\Users\Asus\.eclipse</OtherFolderSettings>
</Folder_Settings>
I would like to take the information inside of OtherFolderSettings and populate it into a listbox. The code I am using sort of works, but it only adds the first two folders strings into the listbox. Thank you in advance for all help and advice.
我想获取OtherFolderSettings中的信息并将其填充到列表框中。我正在使用的代码有点工作,但它只将前两个文件夹字符串添加到列表框中。提前感谢您的所有帮助和建议。
Code:
var applicationSettingsXML = new XmlDocument();
var XMLFileStream = new FileStream("Settings.xml", FileMode.Open);
applicationSettingsXML.Load(XMLFileStream);
var folderList = applicationSettingsXML.GetElementsByTagName("Folder_Settings");
for (var i = 0; i <= folderList.Count; i++)
{
listBox1.Items.Add( applicationSettingsXML.GetElementsByTagName("OtherFolderSettings")[i].InnerText);
}
XMLFileStream.Close();
1 个解决方案
#1
0
The reason your code isn't working is because your loop is:
你的代码不起作用的原因是因为你的循环是:
for (var i = 0; i <= folderList.Count; i++)
... where folderList
is the list of Folder_Settings
elements. There's only one of those, so you're iterating twice. It's a good job you don't actually use folderList[i]
, because otherwise you'd be out of bounds on the second iteration. Loops like that should pretty much always use <
rather than <=
.
...其中folderList是Folder_Settings元素的列表。只有其中一个,所以你要迭代两次。你实际上并没有使用folderList [i],这是一个很好的工作,因为否则你将在第二次迭代中超出范围。这样的循环应该总是使用 <而不是<=。< p>
However, I would strongly recommend using LINQ to XML instead of XmlDocument
- it makes everything much simpler. I'd also recommend using foreach
loops whenever you can, too:
但是,我强烈建议使用LINQ to XML而不是XmlDocument - 它使一切变得更加简单。我也建议你尽可能使用foreach循环:
var doc = XDocument.Load("Settings.xml");
foreach (var element in doc.Root.Elements("OtherFolderSettings))
{
listBox1.Items.Add(element.Value);
}
(Oh, and use using
statements to close resources cleanly...)
(哦,并使用语句来干净地关闭资源......)
#1
0
The reason your code isn't working is because your loop is:
你的代码不起作用的原因是因为你的循环是:
for (var i = 0; i <= folderList.Count; i++)
... where folderList
is the list of Folder_Settings
elements. There's only one of those, so you're iterating twice. It's a good job you don't actually use folderList[i]
, because otherwise you'd be out of bounds on the second iteration. Loops like that should pretty much always use <
rather than <=
.
...其中folderList是Folder_Settings元素的列表。只有其中一个,所以你要迭代两次。你实际上并没有使用folderList [i],这是一个很好的工作,因为否则你将在第二次迭代中超出范围。这样的循环应该总是使用 <而不是<=。< p>
However, I would strongly recommend using LINQ to XML instead of XmlDocument
- it makes everything much simpler. I'd also recommend using foreach
loops whenever you can, too:
但是,我强烈建议使用LINQ to XML而不是XmlDocument - 它使一切变得更加简单。我也建议你尽可能使用foreach循环:
var doc = XDocument.Load("Settings.xml");
foreach (var element in doc.Root.Elements("OtherFolderSettings))
{
listBox1.Items.Add(element.Value);
}
(Oh, and use using
statements to close resources cleanly...)
(哦,并使用语句来干净地关闭资源......)