Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,

I have written a bunch of "Shared Settings" out to an .xml file and I'm trying to read them back, however I'm only getting the first email address and enabled setting. The XML looks like this:
XML
<SharedSettings>
<eMail>
 <eMailAddress>gm111@verizon.net</eMailAddress>
 <eMailAddress>dq222@verizon.net</eMailAddress>
 <eMailAddress>fred333@verizon.net</eMailAddress>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailEnabled>True</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 </eMail>
</SharedSettings>


and the code I'm using to read the settings looks like this:

C#
xmlDoc = new XmlDocument();
xmlDoc.Load(@GetSet.path + @"\SharedSettings.xml");

XmlNodeList xnList = xmlDoc.SelectNodes("/SharedSettings/eMail");
x = 1;
foreach (XmlNode xn in xnList)
{
    string eMailAddress[x] = xn["eMailAddress"].InnerText;
    bool bEmail[x] = Convert.ToBoolean(xn["eMailEnabled"].InnerText);
    x++;
}


Looking at the Count in "xn" it shows as one. I'm new to XML and don't see what I've done wrong. Is the XML wrong or am I reading it wrong? Do you have an example of how to do this?

Thank you,
Glenn
Posted
Comments
PIEBALDconsult 12-Apr-14 12:27pm    
That's all you asked it for.
If the Enabled belongs to the Address, then I would make it an Attribute (or a sub item if you prefer).
Then alter the XPath to "/SharedSettings/eMail/eMailAddress" to get each eMailAddress item.

1 solution

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
XML
<?xml version="1.0" encoding="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)
C#
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#[^]
 
Share this answer
 
v3
Comments
gggustafson 4-May-14 14:13pm    
For completeness, you might want to include the xnList.Count > 0 test in your solution.

+5
CHill60 5-May-14 6:24am    
Good call! On my phone at the moment so not going to chance it :-) Feel free though
gggustafson 5-May-14 10:47am    
done
CHill60 6-May-14 5:06am    
Thank you!
RaisKazi 5-May-14 13:52pm    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900