1
votes

I have a problem to save 2x Datatable into 1 xml file.

Im using c# .Net 4.6 +

DataTable dt = (DataTable)dataGridView1.DataSource;
DataTable dt1 = (DataTable)dataGridView2.DataSource;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "XML|*.xml";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            try
            {
                dt.WriteXml(sfd.FileName);
                dt1.WriteXml(sfd.FileName);
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex);
            }
        }

For loading data im using :

 OpenFileDialog sfd = new OpenFileDialog();
 sfd.Filter = "XML|*.xml";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string file = sfd.FileName;
                try
                {

                    dt.ReadXml(file);
                    dt1.ReadXml(file);

                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex);
                }
            }

I want to save dt and dt1 to xml file then load it by 1 xml file.

How can i save it? When i use my code above then dt1 overwrites dt.

2

2 Answers

0
votes

You can as an example , start by using the XmlDocument class to create your custom XML in conjuection with a TextWriter

    TextWriter writer = null;

                    try
                    {
                        XmlDocument doc = new XmlDocument();

                        //xml decleration
                        var xmlDecleration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");

                        doc.AppendChild(xmlDecleration);

                        //create the 
                        XmlElement rootElement = doc.CreateElement("datatableTopLevelElement");//, HierachyFileConstants.EXPORT_NAMESPACE);//the root element
                        XmlAttribute versionAttribute = doc.CreateAttribute("someVersionAttribute");
                        versionAttribute.Value = Version;//set the version number
                        rootElement.Attributes.Append(versionAttribute);

                        doc.AppendChild(rootElement);

    XmlElement dataObjectType = doc.CreateElement("FirstDataTable");//element name

    using (var ms = new MemoryStream())
                            {
                                var serializer = new XmlSerializer(dataType.GetType());//datatable type(if you know it)
                                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                                ns.Add("", "");
                                writer = new StreamWriter(ms);
                                serializer.Serialize(writer, dataType, ns);

                                //for some reason using xmlSettings with UTF-8 encoding creates an extra unecessary character

                                //remove first line of xml file
                                var xmlData = string.Join(Environment.NewLine, Encoding.UTF8.GetString(ms.ToArray())
                                    .Split(Environment.NewLine.ToCharArray())
                                    .Skip(1)
                                    .ToArray());

                                var xmlElement = GetElement(xmlData);

                                dataObjectType.AppendChild(dataObjectType.OwnerDocument.ImportNode(xmlElement, true));
                            }

//end of first datatable -- repeat from first datable method to add second datatable(
     }
                        }

                    }

                    //var tes
                    //return new XmlDocument().LoadXml(doc.OuterXml.Replace("xmlns=\"" + string.Empty + "\"",string.Empty));
                    var docToReturn = new XmlDocument();
                    docToReturn.LoadXml(doc.OuterXml.Replace("xmlns=\"" + string.Empty + 

    "\"", string.Empty));
                        return docToReturn;
    }


The GetElement method looks like this 

    private XmlElement GetElement(string xml)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
                doc.DocumentElement.Attributes.RemoveNamedItem("xmlns");
                //doc.DocumentElement.NamespaceURI
                return doc.DocumentElement;
            }

Just after adding the first datatable - add the second datatable to add the it to the xml, or create a List of your datatable and do a foreach statement to make it simpler

0
votes

As i said in comment. The best and the easier way is to datatables add in datasets.

Datasets merge in 1 by code :

  DataTable dt = (DataTable)dataGridView1.DataSource;
  DataSet ds = new DataSet();
  DataSet ds1 = new DataSet();
  ds.Tables.Add(dt);
  ds1.Tables.Add(dt1);
                DataTable dt1 = (DataTable)dataGridView2.DataSource;
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "XML|*.xml";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        ds.Merge(ds1);
                        ds.WriteXml(sfd.FileName);
                        //dt1.WriteXml(sfd.FileName);
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine(ex);
                    }
                }

And then load it like this :

   OpenFileDialog sfd = new OpenFileDialog();
        sfd.Filter = "XML|*.xml";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            string file = sfd.FileName;
            try
            {

                dt.ReadXml(file);
                dt1.ReadXml(file);

            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex);
            }
        }