Enverinfure
New Member
I am trying to write values into an XML file using this function. I am retaining the value under sql_connection, but am receiving the error, "Object reference not set to an instance of an object." I understand what the error means, but I do not know how to work with XML files. How should I approach this problem? When I step through my code it stops at myNode.Value = http://stackoverflow.com/questions/15891542/sql_connection; It says I am returning a null value, but sql_connection sees the value I entered on my admin page. Thanks in advance.\[code\] 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 = http://stackoverflow.com/questions/15891542/sql_Connection; myXmlDocument.Save("C:\\Users\\fthompson11\\WebFile.xml"); }\[/code\]I have also tried doing this:\[code\] 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 = http://stackoverflow.com/questions/15891542/sql_Connection; myXmlDocument.Save("C:\\Users\\fthompson11\\WebFile.xml"); }\[/code\]Here you go:\[code\]<?xml version="1.0" encoding="UTF-8"?><!--This is to write the connection string-->-<ConnectionStrings> <add connectionString="asdf" Name="sqlConnection1"/> </ConnectionStrings>\[/code\]