c# - IF statement dilemma -


i'm creating .csv report xml file looks this:

<registration>     <registrationid>1</registrationid>     <type>2</type> </registration> <registration>     <registrationid>2</registrationid>     <type>3</type> </registration> 

how can string value "true", if in xml file there @ least 1 <type>3</type> in <registration> tag, or "false" if there none.

for getting value of each <registrationid> this

string type = "";  foreach (cgxml.registrationrow rr in fisier.registration) {     type = rr.type.tostring     sout.writeline(string.concat(new string[] { "\"", type, "\"" }));  } 

but sadly current skills i'm unable make work each <registrationid> "true/false" statement...

so, code this, or should start reading learn/understand of immense help.

thank time.

welcome stack overflow!

first of xml not valid. if posting question need provide enough info users can use info replicate scenario.

anyhow imagine file:

<?xml version="1.0"?> <stuff>     <registration>         <registrationid>1</registrationid>         <type>2</type>     </registration>     <registration>         <registrationid>2</registrationid>         <type>3</type>     </registration> </stuff> 

here how can read it. basing true or false decision on value of type node. may use code node. can reduce code using linq kept simple clarity. here method return string containing "true" if type node has 2 or 3 in entire file:

public static string searchfile(string path) {     var document = xdocument.parse(file.readalltext(path));      foreach (var thisregistration in document.element("stuff").elements("registration"))     {         var typevalue = thisregistration.element("type").value;         if (typevalue == "2" ||             typevalue == "3")         {             return "true";         }     }      return "false"; } 

here how use code:

var answer = searchfile("pathtoyourfile.xml"); // replace filepath console.writeline("answer: {0}", answer); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -