Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
<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:
 
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 12-Apr-14 6:28am
gmhanna784
Comments
PIEBALDconsult at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 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)
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#[^]
  Permalink  
v3
Comments
gggustafson at 4-May-14 14:13pm
   
For completeness, you might want to include the xnList.Count > 0 test in your solution.
 
+5
CHill60 at 5-May-14 6:24am
   
Good call! On my phone at the moment so not going to chance it :-) Feel free though
gggustafson at 5-May-14 10:47am
   
done
CHill60 at 6-May-14 5:06am
   
Thank you!
RaisKazi at 5-May-14 13:52pm
   
5
RaisKazi at 5-May-14 16:07pm
   
Very well answered.

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 May 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100