How do you separate results by a character when looping through an XML file in C#? -


i storing settings in settings.xml file c# windows forms application , in xml file storing e-mail addresses.

i achieve being able loop through these e-mail addresses , send 1 e-mail of them.

what best way of looping through them , adding them using to.add method of mailmessage class in c#?

i have following code below retrieve them xml file:

var doc = xdocument.load(application.startuppath + "//settings.xml"); stringbuilder result = new stringbuilder(); foreach (xelement c in doc.descendants("emailaddresses"))  {     messagebox.show("results: " + c.value, "test"); }        

i have not been able figure out how split results. results in messagebox so: "email@domain.comemail@domain.comemail@domain.com" , on..or if best way achieve want.

your appreciated!

rather evaluating entire content below "emailaddresses" element, should enumerating child nodes individually. assuming "email<#>" elements children, code similar commenter stribizhev offered should work fine:

foreach(xelement c in doc.descendants("emailaddresses")     .selectmany(x => x.descendantnodes()         .where(‌​x => x.nodetype == system.xml.xmlnodetype.text))) {     messagebox.show("results: " + c.value, "test"); } 

note can't call descendantnodes() on result of call descendants(), return value instance of ienumerable<xelement>, not single xelement. can use selectmany() method flatten enumeration of descendants enumeration of their descendants.

alternatively, check node's name:

foreach(xelement c in doc.descendants("emailaddresses")     .selectmany(x => x.elements().where(‌​x => x.name.startswith("email"))) {     messagebox.show("results: " + c.value, "test"); } 

based on information you've provided far, expect either of work fine.

the above displays values in messagebox, in original example. obviously, can pass c.value mailaddresscollection.add() method instead, add them wanted.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -