Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7583585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:42:56+00:00 2026-05-30T18:42:56+00:00

I am using the following program to update the data which is stored in

  • 0

I am using the following program to update the data which is stored in xml document using C#.

I have the two fields named username and password. If i try to insert the data, it will successfully be added.

My problem is when I try to update the data which is already stored in xml document. I am not able to update the record. So how can I update the record in XLM document using C#?

I have the following exception NullReferenceException which is originated in this line:

root.ReplaceChild(newCd, oldCd);

Button1_Click adds the data, Button2_Click updates the data.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Connection();      
    }

    protected void Connection()
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(@"D:\vijay.net\xmlstorage\XMLFile.xml");
        XmlNode xmlnod = xmldoc.SelectSingleNode("records");
        XmlNode xmlrec = xmlnod.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "record", ""));
        xmlrec.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "Username", "")).InnerText = TextBox1.Text;
        xmlrec.AppendChild(xmldoc.CreateNode(XmlNodeType.Element, "password", "")).InnerText = TextBox2.Text;
        xmldoc.Save(@"D:\vijay.net\xmlstorage\XMLFile.xml");
        Response.Write("Successfully saved in xml file");
        TextBox1.Text = "";
        TextBox2.Text = "";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {    
        XmlTextReader reader = new XmlTextReader(@"D:\vijay.net\xmlstorage\XMLFile.xml");
        XmlDocument doc = new XmlDocument();
        doc.Load(reader);
        reader.Close();

        XmlNode oldCd;
        XmlElement root = doc.DocumentElement;
        oldCd = root.SelectSingleNode("/catalog/cd[Username='" + TextBox1.Text + "']");
        XmlElement newCd = doc.CreateElement("cd");
        newCd.SetAttribute("Password", TextBox2.Text);

        newCd.InnerXml = "<Username>" + this.TextBox1.Text + "</Username>";
        root.ReplaceChild(newCd, oldCd);
         doc.Save(@"D:\vijay.net\xmlstorage\XMLFile.xml");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:42:58+00:00Added an answer on May 30, 2026 at 6:42 pm

    The problem seems to be an incompatibility between the XML document and the XPath expression used to select the node to modify.

    Based on the code in the Connection() method the resulting XML structure will look something like this:

    <records>
        <record>
            <username>SomeUsername</username>
            <password>SomePassword</password>
        </record>
    </records>
    

    In the Button2_Click event handler, you’re selecting the /catalog/cd node based on the value of its Username attribute:

    oldCd = root.SelectSingleNode("/catalog/cd[Username='" + TextBox2.Text + "']");
    

    which returns null because that doesn’t reflect the actual XML structure. You need to change the XPath expression to select the oldCd node based on its content instead:

    oldCd = root.SelectSingleNode("//username[contains(., '" + TextBox2.Text + "')]");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python program that reads floating point values using the following regular
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
I have the following Python 2.6 program and YAML definition (using PyYAML ): import
I have a C++ program which is working quite well using OpenMP to parallelize
I'm using a Java program to POST some XML data from a remote client
I made a program in which I wanted to manually update the Data Grid
I'm trying to start Microsoft word using QProcess as following: QString program = WINWORD.EXE;
I'm having problems compiling the following program. I'm using gcc -framework Foundation inherit8.1m and
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void
I launch the following command line (process) from a Windows VC++ 6 program using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.