Click here to Skip to main content
15,881,631 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Using below code im creating the xml file. My requirement is to create/update XML file , if item already exists then i need to update the item else i need to add that element.
C#
XmlSerializer serializer = new XmlSerializer(typeof(ReleaseChangeModel));
Stream fs = new FileStream(filename, FileMode.OpenOrCreate);
XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode);				
serializer.Serialize(writer, objRelchg);                
writer.Close();

following is xml file generated from the above code after passing Model

http://i58.tinypic.com/2a9bfrp.png[^]

Any help is highly apprecicated.

Thanks and Regards,
Mohammed
Posted
Updated 10-Jan-15 5:27am
v3

Do you know how to read an XML file into "objects" (create a XmlDocument ... or DOM ... from the XML) ? That's what you will need to do to change a specific property or state; and then, you'll need to save the changed DOM back to the file to update the file.

I suggest you start by doing some basic reasearch on Objects-to-XML and XML-to-Objects; there are several good articles here on CodeProject, including: [^].

This CP article has just been published this month: [^]. Unfortunatenly this article does not cover the use of DataContract, which is the newest (included in .NET 3.0 and later), and, imho, best way to move objects to and from XML: [^].
 
Share this answer
 
v2
Comments
Md Athaulla 13-Jan-15 3:31am    
Thanks alot for your kind reply. i have checked the links they are helpful. But My requirement is similar to logging into file. There is some problem in xml writing in the current code. First time it will write, second time if I try to write the title in xml, it is writing out of element.I have used simple xmlreader and xmlwriter functions to accomplish in the present code.

<img src="http://i57.tinypic.com/10crq7s.png" border="0" alt="Image and video hosting by TinyPic"></img>

My xml write code is as below

Collapse | Copy Code
using (StringWriter sw = new StringWriter(sbuilder))
{
using (XmlTextWriter w = new XmlTextWriter(sw))
{
w.WriteStartElement("item");
}
}
Thanks alot for your kind reply. i have checked the links they are helpful. But My requirement is similar to logging into file. There is some problem in xml writing in the current code. First time it will write, second time if I try to write the title in xml, it is writing out of <root> element.I have used simple xmlreader and xmlwriter functions to accomplish in the present code.

<img src="http://i57.tinypic.com/10crq7s.png" border="0" alt="Image and video hosting by TinyPic"></img>

My xml write code is as below

C#
using (StringWriter sw = new StringWriter(sbuilder))
{
    using (XmlTextWriter w = new XmlTextWriter(sw))
    {
        w.WriteStartElement("item");
    }
}
 
Share this answer
 
v2

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