So to prevent you from having to open up another reader, I have come up with the following. I am not particularly fond of it (it feels kind of messy), but it does what you seek. My preference would have been to do some XML deserialization on your config file. Then, you would have actual objects to play with, rather than parsing a file as is currently happening. I can demonstrate an example of that if you like :)
My test file occurs first, followed by the code. It should be evident in both of these excerpts where each goes in its respective file, but if it is not clear, please let me know.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
|
// My sample XML file
data
3
one
two
three
// Code to read the file. Note: The names of the nodes are
// arbitrary. I just copied what the designer had generated
// for my previous settings file (without then "name"
// attribute, as requested!)
while (reader.Read()) // this correlates to your while loop; everything below would be added within your loop
{
if (reader.Name == "ArrayOfString" && reader.IsStartElement())
{
while (reader.Read())
{
if (reader.Name == "string")
{
this.listBox1.Items.Add(reader.ReadInnerXml());
}
else if (reader.Name == "count")
{
this.textBox1.Text = reader.ReadInnerXml();
}
}
}
}
|