Click here to Skip to main content
15,896,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one XML file as follow:
XML
<Employees>
 <Employee>
   <EmpId>10</EmpId>
 </Employee>
 <Employee>
  <EmpId>11</EmpId>
 </Employee>
</Employees>


Using C# I just want to make this file as follow:

XML
<Employees>
 <Employee>
   <EmpId>10</EmpId>
    <EName>AA</EName>
 </Employee>
 <Employee>
  <EmpId>11</EmpId>
   <EName>BB</EName>
 </Employee>
</Employees>


Thank U.
Posted
Updated 22-Nov-13 19:32pm
v2

The XML files you show are very small and simple, so you could use probably the simplest way: read the whole file in a DOM structure by using available XML parser into the DOM, System.Xml.XmlDataDocument:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29.aspx[^].

You parse the file into DOM, add children to DOM, save it to file.

You tag says C#.2.0, but you did not tell us if you can use .NET Framework 3.5 or later. If you can, you have another option:

You can do something similar, if you prefer, with the class System.Xml.Linq.XDocument, supporting LINQ to XML programming:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/vstudio/bb387098%28v=vs.100%29.aspx[^].

—SA
 
Share this answer
 
Comments
Brijesh Kr 23-Nov-13 1:42am    
No issue if you use .Net 3.5
Would you please give the sample example to do the same. But without Linq.
Thank u.
Sergey Alexandrovich Kryukov 23-Nov-13 1:50am    
Why should I write a sample? Documentation explains everything. Write your code by yourself, ask further questions if you fave some problems...
—SA
Brijesh Kr 23-Nov-13 1:53am    
I m not asking you to write code, I just want to know the methodoly or logic, If you can?
Brijesh Kr 23-Nov-13 1:54am    
One more thing, the link you have posted, i already have gone through. Posting a link is very easy. I can show You more than 50 link.
Sergey Alexandrovich Kryukov 23-Nov-13 1:57am    
Stop it. Nobody will want to waste time on something which is not easy on a person with such attitude.
You have done nothing, otherwise you would not ask for help.
Helping lazy people is a waste of time, so please continue on your own.
—SA
XmlDocument doc = new XmlDocument();
           doc.Load("d:\\test\\book.xml");

           XmlElement elmRoot = doc.DocumentElement;

           foreach (XmlNode n in doc.DocumentElement)
           {
               XmlElement newtitle = doc.CreateElement("EName");
               newtitle.InnerText = "SomeText";
              n.AppendChild(newtitle);
           }

            doc.Save("d:\\book.xml");
 
Share this answer
 

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