Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
<pre lang="c++"><pre lang="c#">
Hi,
I am facing problem about adding new record on my xml file.
Let's say I have this plist.xml file:

XML
<Project>
  <ProjectDetail id="0">
    <title>Batman vs Superman</title>
    <platform>TV OS</platform>
    <code>5504</code>
    <dbname>BVSM</dbname>
  </ProjectDetail>
</Project>


What I want is to add new record on that XML file:
- ProjectDetail id = "1"
- title = Cartman and Stan
- platform = TV OS
- code = 1045
- dbname = CAS

So it would looks like this:
XML
<Project>
  <ProjectDetail id="0">
    <title>Batman vs Superman</title>
    <platform>TV OS</platform>
    <code>5504</code>
    <dbname>BVSM</dbname>
  </ProjectDetail>
<ProjectDetail id="1">
    <title>Cartman and Stan</title>
    <platform>TV OS</platform>
    <code>1048</code>
    <dbname>CAS</dbname>
  </ProjectDetail>
</Project>


Anyone can give me insight?

What I have tried:

I googled so many things, but since this is my first work with XML I confuse where to start.

I tried this

XmlElement elem = doc.CreateElement("ProjectDetail");
elem.InnerText = "1";
doc.CreateElement("title");
elem.InnerText = "Cartman and Stan";

root.AppendChild(elem);
doc.Save(frm1.filedata);

But the result is wrong.
Posted
Updated 23-Mar-16 23:07pm
v3
Comments
FARONO 24-Mar-16 4:57am    
you should Google "XmlElement", Serialization or XmlDocument.. I'm sure there are plenty of examples..

(http://stackoverflow.com/questions/9761363/adding-to-xml-file)

There's few ways to achieve that:
1) using System.Xml.Linq classes: XDocument[^] + XElement[^] + XAttribute[^]:
C#
string xcontent = @"<Project>
  <ProjectDetail id='0'>
    <title>Batman vs Superman</title>
    <platform>TV OS</platform>
    <code>5504</code>
    <dbname>BVSM</dbname>
  </ProjectDetail>
</Project>";

//just for example
XDocument xdoc = XDocument.Parse(xcontent);
//you should use:
//XDocument xdoc = XDocument.Load("FullFileName.xml");

XElement xele = new XElement("ProjectDetail", new XAttribute("id", 1),
	new XElement("title", "Cartman and Stan"),
	new XElement("platform", "TV OS"), 
	new XElement("code", 1048),
	new XElement("dbname", "CAS"));

xdoc.Root.Add(xele);
xdoc.Save("FullFileName.xml");


2) using System.Xml classes: XmlReader[^] + XmlWriter[^]

3) using Xml Serialization and Deserialization
Introducing XML Serialization[^]
Examples of XML Serialization[^]
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]

Try!
 
Share this answer
 
v2
Quote:
I confuse where to start.
Start with Google and search an XLM reader/writer library.
Feel free do read documentation and follow tutorials too.
Sorry no time to do search for you.
 
Share this answer
 
Although Xdocument is mostly used, I think in your case Xelement is easier to use, here are some examples: XElement Class Overview[^]

XElement contacts =
new XElement("Contacts",
    new XElement("Contact",
        new XElement("Name", "Patrick Hines"), 
        new XElement("Phone", "206-555-0144"),
        new XElement("Address",
            new XElement("Street1", "123 Main St"),
            new XElement("City", "Mercer Island"),
            new XElement("State", "WA"),
            new XElement("Postal", "68042")
        )
    )
);


C#
// This is an WPF example !
private XElement xmlSource;

C#
// Set the datagrid source to XML file.
this.xmlSource = XElement.Load(this.fileName);
this.DataGrid1.DataContext = this.xmlSource;

C#
this.xmlSource.Save(this.fileName);
 
Share this answer
 
v3
Comments
satrio_budidharmawan 24-Mar-16 5:03am    
How do I load and save my existing xml file?
RickZeeland 24-Mar-16 5:05am    
Look at the section: Serializing XML Trees in https://msdn.microsoft.com/en-us/library/bb387085.aspx
You can serialize the XML tree to a File, a TextWriter, or an XmlWriter.

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