Click here to Skip to main content
13,557,976 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to append a xml file to add a new D100. Here is my xml and code I have so far.

<flp:Tab xmlns:flp=""   Title="Testing">
  <flp:Form Number="0" id="1005" />
  <flp:Rev Time="2013-01-21T15:08:00">
    <flp:Author Name="Brad" Aid="15" />
  <flp:Designs Id="D100">
    <flp:D100 Number="1">
      <flp:Note>In Process</flp:Note>
        <flp:Tester Name="David">
            <flp:Title Number="0" Name="Entry 1">
        <flp:TestGiver Name="James" />

Here is the C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace AppendXml
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

        private void Button_Click_1(object sender, RoutedEventArgs e)

            XmlDocument doc = new XmlDocument();

            //XmlElement root = doc.CreateElement("Tab");

            XmlElement D100 = doc.CreateElement("D100");
            D100.SetAttribute("Number", "2");

            XmlElement Code = doc.CreateElement("Code");
            Code.InnerText = "B";

            XmlElement Documented = doc.CreateElement("Documented");
            Documented.InnerText = "false";

            XmlElement Note = doc.CreateElement("Note");
            Note.InnerText = "Complete";

            XmlElement Tester = doc.CreateElement("Tester");
            Tester.SetAttribute("Name", "John");

            XmlElement Title = doc.CreateElement("Title");
            Title.SetAttribute("Number", "0");
            Title.SetAttribute("Name", "Ronald");

            XmlElement Start = doc.CreateElement("Start");
            Start.InnerText = "Begin";

            XmlElement Finish = doc.CreateElement("Finish");
            Finish.InnerText = "End";

            XmlElement TestGiver = doc.CreateElement("TestGiver");
            TestGiver.SetAttribute("Name", "Jeremy");

            XmlElement IsRequired = doc.CreateElement("IsRequired");
            IsRequired.InnerText = "true";

            XmlElement IsOptional = doc.CreateElement("IsOptional");
            IsOptional.InnerText = "false";





What am I missing? Thanks!!
Posted 9-Feb-13 19:05pm
PIEBALDconsult 10-Feb-13 1:24am
Namespaces cause me trouble; they might be part of the problem.
Are you having trouble appending the D100 in the right spot?
What doesn't seem to be working for you?
mail4bartley 10-Feb-13 1:32am
Well I am trying to add a second D100, and it is not appending anything, it will save the document but when I open it, it is the same as the original.
Jibesh 10-Feb-13 3:29am
Check my solution . You need to find the Node you want to add and trying adding newly created node under it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Ok. here you go.

First step you need to get the Node you want to add your child node, then add your child nodes to this node.

Since you want to Append your child to this Designs Id="D100" node, use the following code to get this node first.
XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("flp", "");

XmlNode nextNode = doc.SelectSingleNode("/flp:Tab/flp:Designs", namespaces);

To create each element you should append the namespace element flp in order to match with other elements, to add namespace to each Element use the following code.
XmlElement D100 = doc.CreateElement("flp","D100","");
D100.SetAttribute("Number", "2");

XmlElement Code = doc.CreateElement("flp", "Code", "");
Code.InnerText = "B";

Please note that you need to re-order the element to you add to the D100 element and add its child nodes like the other node.

finally add the newly created D100 node to the Designs child node
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Before the save you need to add the element to the xml document, for example:

or prhaps

Jibesh 10-Feb-13 2:55am
No this wont work. Check my solution.
Jibesh 10-Feb-13 3:09am
Yes. thats what i pointed out here..and talking about your updated solution, the new new just added to the Root Element and not the Child element that OP is expecting to add.

Also if you check the Node names added to the Xml for the given code, it doesnt append with the name space element "flp". You must use the namespace if you want to add namespace to each node.
Joezer BH 10-Feb-13 3:13am
Did you try using "doc.DocumentElement.AppendChild..."?

About the namespace: you can try to create the xmlNode with the namespace.
e.g. doc.CreateElement("flp:Note") instead of doc.CreateElement("Note")
Jibesh 10-Feb-13 3:16am
Of-course yes. I tried and it wont add "flp:Note" as you element node name. It will add name as "Note" you may try and post if there is any other method where to add this, so that i can also find other way to add it.
Joezer BH 10-Feb-13 3:27am
1. What happens when you try "doc.DocumentElement.AppendChild"? (This should work).
2. You can use Jibesh's suggestion of adding namespace to the node.
mail4bartley 10-Feb-13 9:26am
Thanks guys, I got it to work, I need to mess around with the formatting.! Thanks again! I can post my final code if you want to see it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180515.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100