Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML C#4.0
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="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
 
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 9-Feb-13 20:05pm
Comments
PIEBALDconsult at 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 at 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 at 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
good
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", "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.
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
nextNode.AppendChild(D100);
  Permalink  
Rate this: bad
good
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:
doc.AppendChild(D100);
or prhaps
doc.DocumentElement.AppendChild(D100);
Cheers,
Edo
  Permalink  
v2
Comments
jibesh at 10-Feb-13 2:55am
   
No this wont work. Check my solution.
jibesh at 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.
Edo Tzumer at 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 at 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.
Edo Tzumer at 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 at 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
0 Sergey Alexandrovich Kryukov 545
1 OriginalGriff 285
2 Shweta N Mishra 244
3 BillWoodruff 230
4 Mathew Soji 225
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 10 Feb 2013
Copyright © CodeProject, 1999-2014
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