Click here to Skip to main content
15,915,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends, I have an XML file
XML
<?xml version="1.0" encoding="utf-8" ?>
<HEDDERS>
  <HEDDER>
    <Name>AAAAA</Name>
  </HEDDER>
  <HEDDER>
    <Name>BBBBB</Name>
  </HEDDER>
  <HEDDER>
    <Name>CCCCC</Name>
  </HEDDER>
  <HEDDER>
    <Name>DDDDD</Name>
  </HEDDER>
  <HEDDER>
    <Name>EEEEE</Name>
  </HEDDER>
</HEDDERS>


I want to add a new element
XML
<HEDDER>
    <Name>FFFFFF</Name>
  </HEDDER>

after the last <HEADDER> element.
using ASP.Net C#
Posted
Comments
VJ Reddy 23-May-12 2:10am    
Thank you for accepting the solution :)

Any Ideas......?
....
.
.
.
.
.
.
.
.
.
.
?
 
Share this answer
 
thank you Reddy. It works .. but the output is

XML
<HEDDERS>
  <HEDDER>
    <Name>AAAAA</Name>
  </HEDDER>
  <HEDDER>
    <Name>BBBBB</Name>
  </HEDDER>
  <HEDDER>
    <Name>CCCCC</Name>
  </HEDDER>
  <HEDDER>
    <Name>DDDDD</Name>
  </HEDDER>
  <HEDDER>
    <Name>EEEEE</Name>
  </HEDDER>
  <Name>XXXXXX</Name>
</HEDDERS>


see the last name tag. It comes out HEDDER ..
 
Share this answer
 
I write code like this

C#
XDocument doc;

       //Verify whether a file is exists or not
       if (!System.IO.File.Exists(file))
       {
           doc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
               new System.Xml.Linq.XElement("HEADDERS"));
       }
       else
       {
           doc = XDocument.Load(file);
           XElement ele = new XElement("Name", TextBox1.Text);
           doc.Root.Element("HEADDER").Add(ele);
           doc.Save(file);
       }


But this shows an error. " Object reference not set to an instance of an object."
 
Share this answer
 
v2
Comments
VJ Reddy 23-May-12 2:11am    
Edit: pre tag for C# code added.
VJ Reddy 23-May-12 2:14am    
doc.Root returns the root element i.e. HEDDERS in this case. Hence, doc.Root.Add(ele) will suffice
The following code can be used to add the element
C#
string xmlText = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
                        <HEDDERS>
                        <HEDDER>
                            <Name>AAAAA</Name>
                        </HEDDER>
                        <HEDDER>
                            <Name>BBBBB</Name>
                        </HEDDER>
                        <HEDDER>
                            <Name>CCCCC</Name>
                        </HEDDER>
                        <HEDDER>
                            <Name>DDDDD</Name>
                        </HEDDER>
                        <HEDDER>
                            <Name>EEEEE</Name>
                        </HEDDER>
                        </HEDDERS>";
    XElement element = XElement.Parse(xmlText);

    element.Add(new XElement("HEDDER",
                                     new  XElement("Name","FFFFFF")
                                ));

//<HEDDERS>
//  <HEDDER>
//    <Name>AAAAA</Name>
//  </HEDDER>
//  <HEDDER>
//    <Name>BBBBB</Name>
//  </HEDDER>
//  <HEDDER>
//    <Name>CCCCC</Name>
//  </HEDDER>
//  <HEDDER>
//    <Name>DDDDD</Name>
//  </HEDDER>
//  <HEDDER>
//    <Name>EEEEE</Name>
//  </HEDDER>
//  <HEDDER>
//    <Name>FFFFFF</Name>
//  </HEDDER>
//</HEDDERS>
 
Share this answer
 
v3
Comments
Wendelius 23-May-12 1:40am    
Should work just fine :)
VJ Reddy 23-May-12 1:43am    
Thank you, Mika :)
Mohamed Mitwalli 23-May-12 3:41am    
5+
You can use the AppendChild[^] to add a new child to HEDDERS. Please see the small example in the MSDN documentation.
 
Share this answer
 
Comments
VJ Reddy 23-May-12 1:31am    
Good answer. 5!
I have seen your answer after posting my solution.
Wendelius 23-May-12 1:40am    
Thanks :)
Mohamed Mitwalli 23-May-12 3:41am    
5+

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