c# - Understanding parsing of XML elements -


i have following xml in file.

        <config>         <appsettings>                 <dev>                 <indir>\\serv1\test\indir\</indir>                 <outdir>\\serv1\test\outdir\</outdir>                 <logdir>\\serv1\test\logdir\</logdir>                 </dev>                 <stage>                 <indir>\\serv1\indir\</indir>                 <outdir>\\serv1\outdir\</outdir>                 <logdir>\\serv1\logdir\</logdir>                 </stage>                 <prod>                 <indir>\\serv2\indir\</indir>                 <outdir>\\serv2\outdir\</outdir>                 <logdir>\\serv2\logdir\</logdir>                 </prod>         </appsettings>          <execsettings>                 <dev>                 <filter>*.txt</filter>                 <retention>7</retention> <!-- in days -->                 </dev>                 <stage>                 <filter>*.txt</filter>                 <retention>14</retention> <!-- in days -->                 </stage>                 <prod>                 <filter>*.txt</filter>                 <retention>60</retention> <!-- in days -->                 </prod>             </execsettings>      </config> 

using variable named ‘platform’ holding string “dev” or “stage” or “prod”, want
elements of platform respective part in appsettings , execsettings
, set them in following class.

        public class config     {             public string indir { get; set; }             public string outdir { get; set; }             public string logdir { get; set; }             public string filter { get; set; }             public int32 retention { get; set; }     } 

here code attempt. indicated in comments, counts 0 node
lists. on appreciated.

        xmldocument xdoc = new xmldocument();     string platform = "dev";     ienumerator ienum;     try     {      xdoc.load(xmlfile);     string xpath1 = "appsettings/" + platform;     string xpath2 = "execsettings/" + platform;      xmlnodelist appelements = xdoc.getelementsbytagname("xpath1");  //count = 0     xmlnodelist execelements = xdoc.getelementsbytagname("xpath2"); //count = 0      config cset = new config();      ienum = appelements.getenumerator(); //null     while (ienum.movenext())     {             xmlnode indir = (xmlnode)ienum.current;             xmlnode outdir = (xmlnode)ienum.current;             xmlnode logdir = (xmlnode)ienum.current;             cset.indir = indir.innertext;             cset.outdir = outdir.innertext;             cset.logdir = logdir.innertext;     }      ienum = execelements.getenumerator(); //null     while (ienum.movenext())     {             xmlnode filter = (xmlnode)ienum.current;             xmlnode retention = (xmlnode)ienum.current;             cset.filter = filter.innertext;             cset.retention = retention.innertext;     }   

with feedback i've received (thanks all), here updated code posting, condensed, i'm still seeing count = 0 xmlnodelist.

        xmldocument xdoc = new xmldocument();         string platform = "dev";         try         {                  xdoc.load(xmlfile);                 string xpath1 = "appsettings/" + platform;                 xmlnodelist appelements = xdoc.selectnodes("xpath1");  //count = 0                  config cset = new config();                 foreach (xmlnode node in appelements)                 {                         cset.indir = node["./indir"].innertext;                 }         }         catch (exception ex)         {                 adhoc.func.recordto_logfile("xml||load_failure");                 throw (ex);         } 

how solve problem in different way: instead if prod / dev nodes use xml transformations. supported msbuild , leave config files cleaner.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -