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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:55:15+00:00 2026-05-29T08:55:15+00:00

I’m using XmlDocument and XmlWriter to append XML into an existing file, but my

  • 0

I’m using XmlDocument and XmlWriter to append XML into an existing file, but my attempt below is throwing an exception that I don’t understand

This document already has a ‘DocumentElement’ node.

//Append to xml file

XmlDocument doc = new XmlDocument();
doc.Load(@"c:\\test.xml");
using (XmlWriter xmlWrite = doc.CreateNavigator().AppendChild())
{
    xmlWrite.WriteStartElement("image name=",Name);
    xmlWrite.WriteElementString("width", widthValue[1]);
    xmlWrite.WriteElementString("Height", heightValue[1]);
    xmlWrite.WriteElementString("file-size", FileSizeValue[1]);
    xmlWrite.WriteElementString("file-format", FileFormatValue[1]);
    xmlWrite.WriteElementString("resolution", ResolutionValue[1]);
    xmlWrite.Close();
}

here is my sample test.xml

<job-metadata>
    <slug>730s_Sales/CupWinner_0111</slug>
    <locations>Africa</locations>
    <primary-location>Africa</primary-location>
    <reporter>Leigh Sales</reporter>
    <genre>Current</genre>
    <copyright>CBS</copyright>
    <autopublish>true</autopublish> 
</job-metadata>

Am trying to append in the xml like below

<job-metadata>
    <slug>730s_Sales/CupWinner_0111</slug>
    <locations>Africa</locations>
    <primary-location>Africa</primary-location>
    <reporter>Leigh Sales</reporter>
    <genre>Current</genre>
    <copyright>CBS</copyright>
    <autopublish>true</autopublish> 
    <image name="557684_20111101-730s_SalesCupWinner_0111_80x60.jpg">
        <width>80</width>
        <height>60</height>
        <file-size>7045</file-size>
        <file-format>JPEG Baseline</file-format>
        <resolution>72</resolution>
        <custom-name>newsthumbnail</custom-name>
    </image>
</job-metadata>
  • 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-29T08:55:16+00:00Added an answer on May 29, 2026 at 8:55 am

    To play with the XML data if you are using .net version 3.5 its better to user LINQ to XML.

    http://www.codeproject.com/Articles/24376/LINQ-to-XML

    or

    Manipulate XML data with XPath and XmlDocument (C#)

    OR

    Article : How to Append to a Large XML File

    I thnik you need to append node to your xmldocuemnt like this

    //add to elements collection
    doc.DocumentElement.AppendChild(node);
    

    You need to do something like this

    XmlDocument xmlDoc=new XmlDocument();
    
    xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml");
    
    XmlElement subRoot=xmlDoc.CreateElement("User");
    //UserName
    XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName");
    XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim());
    appendedElementUsername.AppendChild(xmlTextUserName);
    subRoot.AppendChild(appendedElementUsername);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    //Email
    
    XmlElement appendedElementEmail=xmlDoc.CreateElement("Email");
    XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim());
    appendedElementEmail.AppendChild(xmlTextEmail);
    subRoot.AppendChild(appendedElementEmail);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    
    xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");if(!File.Exists("F:/Documents and Settings/Administrator/Desktop/Account.xml"))
    {
    
    XmlTextWriter textWritter=new XmlTextWriter("F:/Documents and Settings/Administrator/Desktop/Account.xml", null); 
    textWritter.WriteStartDocument();
    textWritter.WriteStartElement("USERS");
    textWritter.WriteEndElement();
    
    textWritter.Close();
    }
    
    
    
    XmlDocument xmlDoc=new XmlDocument();
    
    xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml");
    
    XmlElement subRoot=xmlDoc.CreateElement("User");
    //UserName
    XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName");
    XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim());
    appendedElementUsername.AppendChild(xmlTextUserName);
    subRoot.AppendChild(appendedElementUsername);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    //Email
    
    XmlElement appendedElementEmail=xmlDoc.CreateElement("Email");
    XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim());
    appendedElementEmail.AppendChild(xmlTextEmail);
    subRoot.AppendChild(appendedElementEmail);
    xmlDoc.DocumentElement.AppendChild(subRoot);
    
    xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");
    

    The result’ll be like that:

    </USERS>
    <User>
    <UserName>Buggaya</UserName> 
    
    <Email>Buggaya@gmail.com</Email> 
    </User>
    </USERS>
    

    orignal post : Append in xml document

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to count how many characters a certain string has in PHP, but

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.