Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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.

XML
<flp:Tab xmlns:flp="http://www.w3.org/2001/XMLSchema"   Title="Testing">
  <flp:Form Number="0" id="1005" />
  <flp:Rev Time="2013-01-21T15:08:00">
    <flp:Author Name="Brad" Aid="15" />
  </flp:Rev>
  <flp:Designs Id="D100">
    <flp:D100 Number="1">
      <flp:Code>A</flp:Code>
      <flp:Documented>true</flp:Documented>
      <flp:Note>In Process</flp:Note>
      <flp:Testers>
        <flp:Tester Name="David">
          <flp:Titles>
            <flp:Title Number="0" Name="Entry 1">
              <flp:Start>Begin</flp:Start>
              <flp:Finish>End</flp:Finish>
            </flp:Title>
          </flp:Titles>
        </flp:Tester>
      </flp:Testers>
      <flp:TestGivers>
        <flp:TestGiver Name="James" />
      </flp:TestGivers>
      <flp:IsRequired>true</flp:IsRequired>
      <flp:IsOptional>false</flp:IsOptional>
    </flp:D100>
  </flp:Designs>
</flp:Tab>




Here is the C# code

C#
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()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Desktop\\Temp.xml");

            //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";




            D100.AppendChild(IsOptional);
            D100.AppendChild(IsRequired);
            D100.AppendChild(TestGiver);
            D100.AppendChild(Finish);
            D100.AppendChild(Start);
            D100.AppendChild(Title);
            D100.AppendChild(Tester);
            D100.AppendChild(Note);
            D100.AppendChild(Documented);
            D100.AppendChild(Code);

            //root.AppendChild(D100);
            //doc.AppendChild(root);

            doc.Save("test13.xml");



        }
      }
    }


What am I missing? Thanks!!
Posted
Comments
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.

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.
C#
XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
namespaces.AddNamespace("flp", "http://www.w3.org/2001/XMLSchema");

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.
C#
XmlElement D100 = doc.CreateElement("flp","D100","http://www.w3.org/2001/XMLSchema");
D100.SetAttribute("Number", "2");

XmlElement Code = doc.CreateElement("flp", "Code", "http://www.w3.org/2001/XMLSchema");
Code.InnerText = "B";
D100.AppendChild(Code);

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
C#
nextNode.AppendChild(D100);
 
Share this answer
 
Before the save you need to add the element to the xml document, for example:
C#
doc.AppendChild(D100);

or prhaps
C#
doc.DocumentElement.AppendChild(D100);

Cheers,
Edo
 
Share this answer
 
v2
Comments
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900