Couple of points here ...
Firstly your xml. Your
eMail
node can't be relied upon to return the correct enabled setting for each email address ... you essentially have random data in there, but importantly, you only have
one eMail node - hence why your count stays at 1 and your code only reads the first instance each of
eMailAddress
and
eMailEnabled
Structure your xml into sub-nodes ... each "email" has an address AND a enabled setting. Something like this would work
="1.0"="utf-8"
<SharedSettings>
<eMail>
<eMailAddress>gm111@verizon.net</eMailAddress>
<eMailEnabled>True</eMailEnabled>
</eMail>
<eMail>
<eMailAddress>dq222@verizon.net</eMailAddress>
<eMailEnabled>False</eMailEnabled>
</eMail>
<eMail>
<eMailAddress>fred333@verizon.net</eMailAddress>
<eMailEnabled>False</eMailEnabled>
</eMail>
</SharedSettings>
By the way - there is no need for all those empty nodes, they're just taking up space.
Now to your code ... I couldn't even get this to compile - even after I provided a declaration for
xmlDoc
and
x
So I corrected the declarations of eMailAddress and bMail like this (outside the loop)
string[] eMailAddress = new string[xnList.Count];
bool[] bEmail = new bool[xnList.Count];
Note - when you do this you will have to check that
xnList.Count
is not zero
Combined with the changes to the XML above, that all started to work.
For completeness here is the code I ended up with
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"\SharedSettings.xml");
XmlNodeList xnList = xmlDoc.SelectNodes("/SharedSettings/eMail");
if ( xnList.Count > 0 )
{
string[] eMailAddress = new string[xnList.Count];
bool[] bEmail = new bool[xnList.Count];
int x = 1;
foreach (XmlNode xn in xnList)
{
eMailAddress[x] = xn["eMailAddress"].InnerText;
bEmail[x] = Convert.ToBoolean(xn["eMailEnabled"].InnerText);
x++;
}
}
As an aside, you might find XMLReader more efficient if you are just reading straight through xml like this ... have a look at this
Using the XmlReader class with C#[
^]