c# - Writing to an XML file using standard microsoft libraries -


i trying write values xml file using function. retaining value under sql_connection, receiving error, "object reference not set instance of object." understand error means, not know how work xml files. how should approach problem? when step through code stops @ mynode.value = sql_connection; says returning null value, sql_connection sees value entered on admin page. in advance.

    public void savesqlconnection(string sql_connection)     {         xmldocument myxmldocument = new xmldocument();         myxmldocument.load("c:\\users\\fthompson11\\webfile.xml");           xmlnode root = myxmldocument.documentelement;         xmlnode mynode = root.selectsinglenode("/connectionstring");         mynode.value = sql_connection;         myxmldocument.save("c:\\users\\fthompson11\\webfile.xml");     } 

i have tried doing this:

    public void savesqlconnection(string sql_connection)     {         xmldocument myxmldocument = new xmldocument();         myxmldocument.load("c:\\users\\fthompson11\\webfile.xml");          string connectionstringxpath = "/connectionstrings/add[@connectionstring=\"{0}\"]";         connectionstringxpath = string.format(connectionstringxpath, sql_connection);          xmlnode node = myxmldocument.selectsinglenode(connectionstringxpath);         node.attributes["connectionstrings"].value = sql_connection;          myxmldocument.save("c:\\users\\fthompson11\\webfile.xml");     } 

here go:

<?xml version="1.0" encoding="utf-8"?>  <!--this write connection string--> -<connectionstrings> <add connectionstring="asdf" name="sqlconnection1"/>  </connectionstrings> 

it seems want like:

            xmldocument myxmldocument = new xmldocument();              myxmldocument.load(@"..\..\xmlfile1.xml");              xmlnode root = myxmldocument.documentelement;              //we want change 1 connection.              //this removed if want first connection, regardless of name.             var targetkey = "sqlconnection1";              //get add element want             xmlnode mynode = root.selectsinglenode(string.format("add[@name = '{0}']", targetkey));              var sql_connection = "some sql connection";              //set value of connectionstring attribute value want             mynode.attributes["connectionstring"].value = sql_connection;              myxmldocument.save(@"..\..\xmlfile2.xml"); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -