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 8353765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:21:27+00:00 2026-06-09T09:21:27+00:00

i added xml file in my windows application, i want to add values to

  • 0

i added xml file in my windows application, i want to add values to that from textbox..
i used the following code,

string path = "codedata.xml";
        XmlDocument doc = new XmlDocument();
        if (!System.IO.File.Exists(path))
        {
            //Create neccessary nodes
            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            XmlComment comment = doc.CreateComment("This is an XML Generated File");
            doc.AppendChild(declaration);
            doc.AppendChild(comment);
        }
        else //If there is already a file
        {
            //    //Load the XML File
            doc.Load(path);
        }

        //Get the root element
        XmlElement root = doc.DocumentElement;

        XmlElement Subroot = doc.CreateElement("data");
        XmlElement Companycode = doc.CreateElement("Companycode");
        XmlElement Productcode = doc.CreateElement("Productcode");
        XmlElement Productname = doc.CreateElement("Productname");
        XmlElement Brandcode = doc.CreateElement("Brandcode");
        XmlElement Brandname = doc.CreateElement("Brandname");

        Companycode.InnerText = txt_companycode.Text;
        Productcode.InnerText = txt_productcode.Text;
        Productname.InnerText = txt_productname.Text;
        Brandcode.InnerText = txt_brandcode.Text;
        Brandname.InnerText = txt_brandname.Text;

        Subroot.AppendChild(Companycode);
        Subroot.AppendChild(Productcode);
        Subroot.AppendChild(Productname);
        Subroot.AppendChild(Brandcode);
        Subroot.AppendChild(Brandname);
        root.AppendChild(Subroot);
        doc.AppendChild(root);


        //Save the document
        doc.Save(path);


        //Show confirmation message
        MessageBox.Show("Details  added Successfully");

it showing error near root.AppendChild(Subroot); can any one help me, wer i made mistake.

  • 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-06-09T09:21:28+00:00Added an answer on June 9, 2026 at 9:21 am

    The root is null. Try to add Root element when you create XML file.

     if (!System.IO.File.Exists(path))
            {
                //Create neccessary nodes
                XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                XmlComment comment = doc.CreateComment("This is an XML Generated File");
                doc.AppendChild(declaration);
                doc.AppendChild(comment);
                doc.AppendChild(doc.CreateElement("Root"));
            }
    

    Or use LINQ-XML

    string _file=@"c:\sample.xml";
    XDocument doc;
    
    if (!File.Exists(_file))
    {
        doc = new XDocument();
        doc.Add(new XElement("Root"));
    }
    else
    {
        doc = XDocument.Load(_file);
    }
    
    doc.Root.Add(
          new XElement("data",
                       new XElement("CompanyCode","C101"),
                       new XElement("ProductCode","P101")
                ) 
          );
    doc.Save(_file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use an xml file in a windows forms application. I added
I am working on an android application and I added a xml layout file
I added a colors.xml to my resources and wrote the following xml code :
I added a custom section to my app.config file for a Windows Forms Application.
in my web.xml i want to read the welcome file from property file something
I added some UI elements to the main.xml file in the res\layout folder and
I added arrays on the string.xml , and these strings are visible in my
I have added an easy compound control which loads its layout from a XML
I've created a windows application in c# now i added a new setup project
i have the following situation : with my function i get an XML file

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.