c# 4.0 - Parsing a "complex" XML file -



c# 4.0 - Parsing a "complex" XML file -

i'm working on code read , parse nodes of xml file, , layout of file kind complex (at least, seems way me - having not done , xml parsing until project).

the format xml files similar this:

<parameters> <general> <name>name of parameter</name> <caption>information parameter</caption> <groupname>a little grouping of parameters</groupname> <value>1</value> <type>boolean</type> <description>some description</description> <dependencies> <and> <groupname>some other grouping of parameters</groupname> <name>the parameter 1 dependent on</name> </and> <or> <groupname>some other grouping of parameters</groupname> <name>the parameter 1 might dependent on</name> </or> </dependencies> </general> </parameters>

i'm parsing file in manner:

xmldocument xdoc = new xmldocument(); assembly _assembly = assembly.getexecutingassembly(); stream s = _assembly.getmanifestresourcestream(@"nameofassembly.nameoffile.xml"); xdoc.load(s); xmltests = xdoc.selectnodes("/parameters/general"); foreach (xmlnode roottype in xmltests) { switch (roottype.name.tostring()) { case "general": name = roottype["name"].innertext; caption = roottype["caption"].innertext; groupname = roottype["groupname"].innertext; mandatory = roottype["mandatory"].innertext; value = convert.toint32(roottype["value"].innertext); typeofdata = roottype["type"].innertext; description = roottype["description"].innertext; //groupdepand = roottype["dependencies/and/groupname"].innertext; //xmlnodelist dependencieslist = roottype.selectnodes("parameters/general/dependencies/and"); //foreach (xmlnode dependenciesandnode in dependencieslist) //{ //groupdepand = dependenciesandnode["groupname"].innertext; //namesdepand = dependenciesandnode["names"].innertext; //} //dependencieslist = roottype.selectnodes("parameters/general/dependencies/or"); //foreach (xmlnode dependenciesornode in dependencieslist) //{ //groupdepor = dependenciesornode["groupname"].innertext; //namesdepor = dependenciesornode["names"].innertext; //} /* send values object constructor */ break; default: break; } } /*close stream, xdoc , such */

my problem arises when i'm parsing (or attempting parse, case may be) dependencies nodes. i'm not exclusively sure on how parse each of them (groupname , name and, groupname , name or) string. i've commented out lines don't seem work how want them to.

the foreach loops i've commented out, read through mentions of dependencies in general node list. , line starts "groupdepand = " (line 18) fails giving me null reference exception.

am attempting parse xml file correctly? doing wrong? , help much appreciated. said earlier, experience xml limited.

the problems you're having solved these fixes:

//this selects value of first 'and' node, selectsinglenode lets utilize xpath groupdepand = roottype.selectsinglenode("dependencies/and/groupname").innertext; //you using wrong xpath collection, roottype @ /parameters/general xmlnodelist dependencieslist = roottype.selectnodes("dependencies/and");

hope helps, it's worth noting in loop need if there multiple , or or nodes, @ moment you're overwriting old values.

one other thing i'd add together can utilize newer xdocument instead of xmldocument. people much prefer it's more linq-esque. it's john skeet approved:

xdocument or xmldocument

xml c#-4.0

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -