Parsing XML File To Strings in C# -


i'm writing program , i'm trying grab information xml file, break , store information in 4-5 different strings. here's code have grab xml file.

private void getversionxml() {     sversionconfigpath = slocationkey + "core\\config.xml"; //path xml file     xmldocument xmldoc = new xmldocument();     xmldoc.load(sversionconfigpath); //load config.xml     xmlnodelist xmlnodes = xmldoc.getelementsbytagname("primarydatabase");     foreach (xmlnode xmlnode in xmlnodes)     {         label1.text = xmlnode.selectsinglenode("database").innertext; //breaks here     }  } 

here xml file looks like.

<progxml>     <primarydatabase updating="n">         <database driver="sql server" server="servername" user="sa" password="iyp4kvris7orl+njkhijvg==" database="dbname" owner="" port="" uncbase="" impuser="" />         <dataexists action="reset" when="5/20/2015 3:17:36 pm" />         <tablecollection name="core" who="cpuser" processid="0" processname="" status="complete" when="5/20/2015 3:17:47 pm" layoutversion="39" dataversion="39" />         <tablecollection name="prog" who="cpuser" processid="0" processname="" status="complete" when="5/20/2015 3:17:47 pm" layoutversion="38" dataversion="38" />     </primarydatabase> </progxml> 

essentially i'm trying shows in <database /> tag , store string (or in case, print label debugging purposes).

however code breaks commented above nullreferenceexception "object reference not set instance of object". , i'm not quite sure went wrong. appreciated. thanks.

your database node has no value, attributes such driver, server, ...

to retrieve attribute value:

 string driver = xmlnode.selectsinglenode("database").attributes["name"] ; 

Comments