c# - Iterating through a list of XML Elements -


i place follow-up asked question apparently i'm not allowed continue in same thread.

in previous question (iterating through list of elements inside element specific index) looking value of of urls in xml. proper way access different values inside same node? example:

<list index="1" name="" title="" id="5702f74a-9df2-12e5-89e0-f947f6dd0a1f">   <theme>     <properties>       <field index="1"/>       <field index="2" name="title"></field>     </properties>     <list>       <item id="f391fada-90c5-f239-a836-25ca76311286">          <field index="1">            <url>49e424a8ae1bf4707bf4d27c4614e905.png</url>            <title></title>          </field>          <field index="2" name="question">this question</field>          <field index="3" name="answer">this answer</field>          <field index="4" name="remark"/>          <field index="5" name="reveal"/>        </item>         <item id="a0b97163-d195-4dce-b970-ecb6eb080403">          <field index="1">             <url>49e424a8ae1bf4707bf4d27c4614e905.png</url>             <title></title>          </field>          <field index="2" name="question"/>          <field index="3" name="answer"></field>          <field index="4" name="remark"/>          <field index="5" name="reveal"/>        </item>      </list>   </theme> </list> 

i read xml piece piece (item item in xml) , store values of fields specific names variables: example:

string url = [value of field url]; string question= [value of field name question]; string answer= [value of field name answer]; string remark= [value of field name remark]; string reveal= [value of field name reveal]; 

do have use every variable sortlike query?

var string = xmldoc.descendants("list")     .where(e => (int)e.attribute("index") == 1)     .descendants("item").descendants("field")     .where(e => (string)e.attribute("index") == "1")     .select(e => (string)e.value()); 

or there way fields in sort of list can search it's indexnumber?

foreach (field in fields) {   switch case (field.name)   {     case "question":       question = field.value;       break;      case "answer":       answer= field.value;       break;   } } 

any ideas appreciated.

try this. url field doesn't contain name or index tag have test name tag not equal null.

using system; using system.collections.generic; using system.linq; using system.text; using system.xml; using system.xml.linq;  namespace consoleapplication2 {     class program     {         static void main(string[] args)         {             string input =                  "<list index=\"1\" name=\"\" title=\"\" id=\"5702f74a-9df2-12e5-89e0-f947f6dd0a1f\">\n" +                    "<theme>\n" +                      "<properties>\n" +                        "<field index=\"1\"/>\n" +                        "<field index=\"2\" name=\"title\"></field>\n" +                      "</properties>\n" +                      "<list>\n" +                        "<item id=\"f391fada-90c5-f239-a836-25ca76311286\">\n" +                           "<field index=\"1\">\n" +                             "<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>\n" +                             "<title></title>\n" +                           "</field>\n" +                           "<field index=\"2\" name=\"question\">this question</field>\n" +                           "<field index=\"3\" name=\"answer\">this answer</field>\n" +                           "<field index=\"4\" name=\"remark\"/>\n" +                           "<field index=\"5\" name=\"reveal\"/>\n" +                         "</item>\n" +                          "<item id=\"a0b97163-d195-4dce-b970-ecb6eb080403\">\n" +                           "<field index=\"1\">\n" +                              "<url>49e424a8ae1bf4707bf4d27c4614e905.png</url>\n" +                              "<title></title>\n" +                           "</field>\n" +                           "<field index=\"2\" name=\"question\"/>\n" +                           "<field index=\"3\" name=\"answer\"></field>\n" +                           "<field index=\"4\" name=\"remark\"/>\n" +                           "<field index=\"5\" name=\"reveal\"/>\n" +                         "</item>\n" +                       "</list>\n" +                    "</theme>\n" +                  "</list>\n";              xdocument doc = xdocument.parse(input);             var results = doc.descendants("item")                 .select(w => new {                     url = w.descendants("url").firstordefault().value,                     question = w.descendants("field").where(x => x.attribute("name") != null).where(y => y.attribute("name").value == "question").select(z => z.attribute("index").value).firstordefault(),                     answer = w.descendants("field").where(x => x.attribute("name") != null).where(y => y.attribute("name").value == "answer").select(z => z.attribute("index").value).firstordefault(),                     remark = w.descendants("field").where(x => x.attribute("name") != null).where(y => y.attribute("name").value == "remark").select(z => z.attribute("index").value).firstordefault(),                     reveal = w.descendants("field").where(x => x.attribute("name") != null).where(y => y.attribute("name").value == "reveal").select(z => z.attribute("index").value).firstordefault()                 })                 .tolist();         }     } } ​ 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -