0

As per following code I have created xml file and writing on it iteratively. But the problem is it is overwriting on the file. I want to append new element to file, here the foreach is iteratively writing contents of wrobj to xml, but again overwriting

XmlTextWriter xtWriter = new XmlTextWriter("demoxml.xml",Encoding.UTF8);
xtWriter.Formatting = System.Xml.Formatting.Indented;
xtWriter.WriteStartElement("Selected-Workspace");
xtWriter.WriteStartElement("Context-Details");

xtWriter.WriteStartElement("Conid");
xtWriter.WriteString(it.id);
xtWriter.WriteEndElement();

xtWriter.WriteStartElement("Attributes"); //attributes started

xtWriter.WriteStartElement("name");
xtWriter.WriteString(it.attributes.name);
xtWriter.WriteEndElement();
xtWriter.WriteStartElement("organizationReference");
xtWriter.WriteString(it.attributes.organizationReference);
xtWriter.WriteEndElement();
xtWriter.WriteStartElement("organizationReferenceName");
xtWriter.WriteString(it.attributes.organizationReference_name);
xtWriter.WriteEndElement();
xtWriter.WriteStartElement("type");
xtWriter.WriteString(it.attributes.type);
xtWriter.WriteEndElement();

xtWriter.WriteEndElement();// End Attributes
foreach (WorkspaceRootObject wro in wrObj) 
{
    xtWriter.WriteStartElement("Workspace-Details"); // Workspace start

    xtWriter.WriteStartElement("WorkspaceId");
    xtWriter.WriteString(wro.id);
    xtWriter.WriteEndElement();
    xtWriter.WriteStartElement("name");
    xtWriter.WriteString(wro.name);
    xtWriter.WriteEndElement();
    xtWriter.WriteStartElement("description");
    xtWriter.WriteString(wro.description);
    xtWriter.WriteEndElement();
    xtWriter.WriteStartElement("context-id");
    xtWriter.WriteString(wro.contextId);
    xtWriter.WriteEndElement();

    xtWriter.WriteEndElement(); //End Workspace
}

xtWriter.WriteEndElement();
xtWriter.WriteEndElement();
xtWriter.Flush();
xtWriter.Close();

how do I append contents of wrobj object to file.

  • You should take a look at the [XmlSerializer-Class](https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx) – Hyarus Apr 11 '18 at 06:42
  • Or just LINQ to SQL - is there any reason you're using `XmlTextWriter` at all? – Jon Skeet Apr 11 '18 at 06:46
  • i am beginner to this so i did not have knowledge of this – Rohit Binjagermath Apr 11 '18 at 06:52
  • Whoops, I didn't mean LINQ to SQL - I meant LINQ to XML. Huge difference! I would definitely recommend reading a LINQ to XML tutorial... it's much, much simpler than building XML like this. – Jon Skeet Apr 11 '18 at 07:21

1 Answers1

0

When you create the file for the first time do as you have done above. The next time, check if the file exists, and use XDocument to append as given in the here

SajuPK
  • 97
  • 1
  • 10
  • Why use a mixture of techniques? Surely it would be a lot simpler to use `XDocument` in both cases: if the file exists, load that. Otherwise, create a new `XDocument`. Either way, you then add the appropriate elements and save. – Jon Skeet Apr 11 '18 at 07:22
  • Does XmlSerializer overwrites contents to xml file while serializing list of object into xml – Rohit Binjagermath Apr 11 '18 at 11:50