1

the xml file my code is generating is as follows

<?xml version="1.0"?>
<DataClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <pathI>D:\POC\Input\2</pathI>
  <pathO>D:\POC\Output</pathO>
  <prefix>2_</prefix>
  <frequency>25</frequency>
</DataClass><?xml version="1.0"?>
<DataClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <pathI>D:\POC\Input\3</pathI>
  <pathO>D:\POC\Output</pathO>
  <prefix>3_</prefix>
  <frequency>33</frequency>
</DataClass>

I want to add a root element to the xml so that i can further use the xml to populate a data grid view. if possible also want to eliminated tag from every node.. help needed

DataClass data = new DataClass();
data.pathI = txt_input.Text;
data.pathO = txt_Output.Text;
data.frequency = Convert.ToInt32(txt_freq.Text);
data.prefix = txt_prefix.Text;
XmlDocument doc = new XmlDocument();


XmlSerializer xs = new XmlSerializer(typeof(DataClass));
if (!File.Exists("Data.xml"))
{
    using (FileStream fs = new FileStream("Data.xml", FileMode.Create))
    {
         xs.Serialize(fs, data);
         fs.Close();
         fs.Dispose();
         MessageBox.Show("Data loaded to the xml");
    }
}
else if (File.Exists("Data.xml"))
{
     using (FileStream fs = new FileStream("Data.xml",FileMode.Append))
     {
          xs.Serialize(fs, data);
          fs.Close();
          fs.Dispose();
          MessageBox.Show("Data loaded to the xml");
     }
}
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
user2038650
  • 73
  • 1
  • 10

1 Answers1

1

I do not know a way to append objects this way through serialization. The only alternative I know is to serialize an array of objects. This would look like:

DataClass[] objects = ...//get all your objects
if(xs == null) 
{
    xs = new XmlSerializer(typeof(DataClass[]), 
                           new XmlRootAttribute("Your root name"));
}
using (FileStream fs = new FileStream("Data.xml", FileMode.Create))
{
    xs.Serialize(fs, data);
    fs.Close();
}

Consider declaring the serializer static (read Identify And Prevent Memory Leaks In Managed Code to understand why):

private static readonly XmlSerializer xs;

However, if you're open to using Linq to Xml instead, you could get the functionality you need. However each time you need to modify the xml, you would have to load whole xml into memory.

XElement x;
if (File.Exists("Data.xml"))
    x = XElement.Load("Data.xml");
else
    x = new XElement("Data");
x.Add(new XElement("DataClass",
                    new XElement("pathI", @"D:\POC\Input\2"),
                    new XElement("pathO", @"D:\POC\Output"),
                    new XElement("prefix", "2_"),
                    new XElement("frequency", "25")));
x.Save("Data.xml");

Thanks to the link given by Arie (Serialise object to XmlDocument), you may do the following:

XmlDocument temp = new XmlDocument();   //create a temporary xml document
var navigator = temp.CreateNavigator(); //use its navigator
using (var w = navigator.AppendChild()) //to get an XMLWriter
    xs.Serialize(w, data);              //serialize your data to it

XmlDocument xdoc = new XmlDocument();   //init the main xml document
string filename = "Data.xml";
if (File.Exists(filename))              //if file exists
    xdoc.Load(filename);                //load xml from it
else                                    //or 
{
    //add xml declaration to the top of the new xml document
    xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "utf-8", null));
    //create the root element
    xdoc.AppendChild(xdoc.CreateElement("Data"));
}

var newchild = xdoc.CreateElement("DataClass"); //the new element
newchild.InnerXml = temp.FirstChild.InnerXml;   //copy the serialized content

//append the new element to the root
xdoc.ChildNodes[1].AppendChild(newchild);       
//save the document
xdoc.Save(filename);
Community
  • 1
  • 1
horgh
  • 17,918
  • 22
  • 68
  • 123