Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting to an XML file using standard microsoft libraries
    primarykey
    data
    text
    <p>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 = 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.</p> <pre><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 = sql_Connection; myXmlDocument.Save("C:\\Users\\fthompson11\\WebFile.xml"); } </code></pre> <p>I have also tried doing this:</p> <pre><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 = sql_Connection; myXmlDocument.Save("C:\\Users\\fthompson11\\WebFile.xml"); } </code></pre> <p>Here you go:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!--This is to write the connection string--&gt; -&lt;ConnectionStrings&gt; &lt;add connectionString="asdf" Name="sqlConnection1"/&gt; &lt;/ConnectionStrings&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload