locked
Save DataGridView to xml

    Question

  • Hello All
    This issue has got to do with saving and retrieving data from xml into DataGridViews and vice versa
    My xml file would look like this
     

    <?
    xml version ="1.0" encoding="utf-8" ?>
    - <TestDescription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" DeviceName="Three phase meter-1" DeviceType="DeltaPlus1" DescriptionId="79AB3142-38CD-423d-A0CB-4D49D3E75F13" Status="NotStarted" Version="1.0" TimeStamp="2002-10-10T02:00:00+05:30" xmlns="urn:DeviceTestDescription">
    - <CommunicationDescription>
    <PhysicalMedia>RS232</PhysicalMedia>
    <CommunicationProtocol>Modbus</CommunicationProtocol>
    <DeviceAddress>1</DeviceAddress>
    <CommunicationPort>1</CommunicationPort>
    <BaudRate>4800</BaudRate>
    <HandShake>XON</HandShake>
    </CommunicationDescription>
    - <TestSuite>
    - <TestGroup ID="1001" Description="Group1" ErrorCondition="Skip">
    - <TestCase ID="1001" Description="CT">
    - <TestStep Command="SetCT" ID="" Description="Set CT">
    - <Parameters>
    <Parameter Name="setCTRatio" Value="8232" Direction="Write" />
    </Parameters>
    </TestStep>
    - <TestStep Command="ReadCT" ID="" Description="Get CT">
    - <Parameters>
    <Parameter Name="getCTRatio" Direction="Read" />
    </Parameters>
    </TestStep>
    <ValidationCriteria>if setCTRatio == getCTRatio testResult.valid = true else testResult.valid = false end</ValidationCriteria>
    <ValidationError />
    </TestCase>
    </TestGroup>
    </TestSuite>
    <Log>Test Executed by : User1 Test Type : FTT-POC Remarks : DEMO version. Usage permitted for demonstration purposes only.</Log>
    </TestDescription>
     
    As you see above there are sections for Descriptions , CommunicationDescription, TestSteps and TestParamters
    For each of the above sections i have a separate data grid view  and a separate dataset (ds , ds1, ds2, ds3) in my app. I am able to populate the data grid views properly but i am unable to save the modified datagridview into xml . How should i do this ? And since the data is spread over many data grid views , this is more confusing coz i have many data sets each pertaining to one datagridview.
    I made a few changes to the "communicationDescription" and used  "ds1.AcceptChanges() ". It did not save . Using WriteXML () did save but the file got completely erased . Check my code .. Dont think it is any where close to correct
     
    if (ds1.HasChanges() || ds2.HasChanges() || ds3.HasChanges() || ds.HasChanges())
    {
    DescriptionMode mode = (DescriptionMode)Enum.Parse(typeof(DescriptionMode), CurrentNavigation);
    string[] files = Directory.GetFiles("D:\\TFS\\Code Snippets\\TestController\\TestController\\bin\\Debug\\" + mode.ToString(), "*.xml");
    String file = files[overviewClickedRowIndex];
    ds1.WriteXml(file);
    ds2.WriteXml(file);
    ds3.WriteXml(file);
    ds.WriteXml(file);
    }

    Any help will be greatly appreciated. Thanks! Please respond asap..
    AVink
    Wednesday, January 06, 2010 9:52 AM

Answers

  • can you try like this.

    [C#]
    custDS.WriteXml("Customers.xml", XmlWriteMode.WriteSchema);
    [C#]
    System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xml");
    custDS.WriteXml(xmlSW, XmlWriteMode.WriteSchema);
    xmlSW.Close();

    before you do this, create a Dataset object and save your changes to the DataTable. and DataSet. Use this dataset object for writing.

    here is more details : http://msdn.microsoft.com/en-us/library/zx8h06sz(VS.71).aspx
    Thanks Mike --------Please mark as answer if it is useful----------
    • Marked as answer by Bin-ze Zhao Friday, January 08, 2010 9:25 AM
    Friday, January 08, 2010 4:06 AM
  • why you need 4 datasets?, why don't you save it into 4 datatables and add it to a single dataset. this will avoid lot of complexity. Once you done that, write it to XML. Simple
    Thanks Mike --------Please mark as answer if it is useful----------
    • Marked as answer by AVink Wednesday, January 20, 2010 6:47 AM
    Friday, January 08, 2010 10:45 AM

All replies

  • Avink,

    I'm by no means a XML guy, but I did play with a few of the .net library commands for it.

    Below is a sample I used to write a file with XML, there may be faster ways if a specific schema is applied, but that was deeper then i wanted/needed to go.

    I hope this leads you to at least the write help location.

            s.Indent = True
            s.NewLineOnAttributes = True
            Dim f As XmlWriter = XmlTextWriter.Create(n, s)
    
            Console.WriteLine("XML start write...")
    
            f.WriteStartDocument()
            f.WriteStartElement("myApp", "Console_VS2010")
            f.WriteElementString("Firstname", "kyle j")
            f.WriteElementString("lastname", "Eppley")
            f.WriteElementString("n", n)
            f.WriteElementString("f", f.ToString)
            f.WriteStartElement("xmlsettings", s.Indent.ToString)
    
            f.WriteEndElement()
            f.WriteEndElement()
            f.WriteEndDocument()
            f.Flush()
            f.Close()

    -QuickC
    Thursday, January 07, 2010 10:03 PM
  • can you try like this.

    [C#]
    custDS.WriteXml("Customers.xml", XmlWriteMode.WriteSchema);
    [C#]
    System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xml");
    custDS.WriteXml(xmlSW, XmlWriteMode.WriteSchema);
    xmlSW.Close();

    before you do this, create a Dataset object and save your changes to the DataTable. and DataSet. Use this dataset object for writing.

    here is more details : http://msdn.microsoft.com/en-us/library/zx8h06sz(VS.71).aspx
    Thanks Mike --------Please mark as answer if it is useful----------
    • Marked as answer by Bin-ze Zhao Friday, January 08, 2010 9:25 AM
    Friday, January 08, 2010 4:06 AM
  • Hi

    I m sorry but it  did not work . May be coz the data of a single file is distributed over multiple data gridviews on different panels of a custom control , the problem seems tricky .
    But I would like to restate the problem differently in  more simpler words

    My problem statement would be this :

    I have a Custom Control which is a tabbed panel.

    I have an XML doc as my backend to my application

    The XML doc has different sections say "Description", "Communication", "Steps" and "Parameters"

    Each of these sections i display in 4 different datagridviews on 4 different tabs of the tabbed panel. Hence for each of these sections i have a separate data set -which would be ds1, ds2, ds3 and ds4 respectively;

    I am able to populate all the panes properly with doin a ReadXML (filename)

    My issue is that i am not able to save sections of the file when i update the respective panes. Only the first pane updates without issues , the rest of them erase my entire xml file when i perform a save .. How should i selectively update the file ? and Since the data of a single file is distributed over many panels , i want to be able to use save button to selectively save the modifications on the currently active pane.

    :-) How should i do this ? I read the article "Reading and Writing XML in C#" . But still answers eludes me.

    Friday, January 08, 2010 9:50 AM
  • why you need 4 datasets?, why don't you save it into 4 datatables and add it to a single dataset. this will avoid lot of complexity. Once you done that, write it to XML. Simple
    Thanks Mike --------Please mark as answer if it is useful----------
    • Marked as answer by AVink Wednesday, January 20, 2010 6:47 AM
    Friday, January 08, 2010 10:45 AM