How to enable DataGridView updates for xpath extract results

Hi,

I wrote a program to extract part of XML elements, fill in the dataset with extracted elements,and display them in dataGridView (C++). for example : to extract only ADDRESSES and display the Id,Street,Postcode, city and country as the table columns in dataGridView

<MEMBER>
<GENDER>FEMALE</GENDER>
<AGE>35</AGE>
- <ADDRESSES>
- <ADDRESS>
<ID>1</ID>
<STREET>cranbrook ave</STREET>
<POSTCODE>1231231</POSTCODE>
<CITY>NOTTINGHAM</CITY>
<COUNTRY>UK</COUNTRY>
</ADDRESS>
- <ADDRESS>
<ID>2</ID>
<STREET>Wisteria Lane</STREET>
<POSTCODE>2452434</POSTCODE>
<CITY>FAIRVIEW</CITY>
<COUNTRY>US</COUNTRY>
</ADDRESS>
</ADDRESSES>
</MEMBER>


I have done the extraction with Xpath, append the extracted xml results node list with Stringbuilder and display them first in a text box ; then a dataset read the text box and display the result in dataGridView.

However, i would like to enable users edit the dataGridView and save the changes to the original Xml file. Could anyone suggest how I could get around it I am a newbie and I hope this is not too trivia

Many thanks in advance,

Sharvie




Answer this question

How to enable DataGridView updates for xpath extract results

  • abo_selaiman

    You should be able to load XML to XmlDataDocument, create DataView over it and map this view to DataGrid.

  • Miha Stajdohar

    Hello,

    Many thanks for you reply. I managed to display the extracted element (addresses) in DataGrid, but could not figure out a way to update the original xml file if the dataGrid is updated.

    For example, addressTry.xml :

    <MEMBER>
    <GENDER>FEMALE</GENDER>
    <AGE>35</AGE>
    - <ADDRESSES>
    - <ADDRESS>
    <ID>1</ID>
    <STREET>cranbrook ave</STREET>
    <POSTCODE>1231231</POSTCODE>
    <CITY>NOTTINGHAM</CITY>
    <COUNTRY>UK</COUNTRY>
    </ADDRESS>
    </ADDRESSES>

    If users change the CITY from "nottingham" to"London" in Datagrid, how could I update that to addressTry.xml

    I tried loading the original file, delete the <ADDRESS> and child elements, load the DataSet (mapped to DataGrid) that holds the updated addresses data and tried to use xpath navigator to AppendChild

    eg: ( this code comes after I delete the original <ADDRESS> and its child elements marked in red ).

    XmlNodeList^ odlistnew = xml_data2->SelectNodes("//ODS");

    XmlDocument^ doc2 = gcnew XmlDocument();
    doc2->Load("C:\\Try\\addressTry.xml");
    XPathNavigator^ nav = doc2->CreateNavigator();
    XPathNavigator^ ods = nav->SelectSingleNode("/ADDRESSES");
    for(int i=0; i<odlistnew->Count ;i++)
    {
    ods->AppendChild(odlistnew->Item(i)->ToString());
    }

    "xml_data2" is XmlDataDocument that contains the updated <ADDRESSES> and its child elements. ie :

    ADDRESSES>
    - <ADDRESS>
    <ID>1</ID>
    <STREET>cranbrook ave</STREET>
    <POSTCODE>1231231</POSTCODE>
    <CITY>LONDON</CITY>
    <COUNTRY>UK</COUNTRY>
    </ADDRESS>
    </ADDRESSES>

    This does not work and I am wondering why or whether there is any other more direct alternatives to update the original file.

    Many many thanks,

    Sharvie



  • Bruce

    I'm assuming you are able to successfully load the data in DataSet and bind it to DataGridView.

    For instance:

    <ADDRESS>
    <ID>1</ID>
    <STREET>cranbrook ave</STREET>
    <POSTCODE>1231231</POSTCODE>
    <CITY>LONDON</CITY>
    <COUNTRY>UK</COUNTRY>
    </ADDRESS>

    Based on the above XML, the <ADDRESS> element would map to a DataTable with name "ADDRESS" and <City> would map to a DataColumn named City.

    Then one should be able to modify City using myDataSet.Tables["ADDRESS"].Rows["CITY"]="newCityValue" and then either do DataSet.WriteXml to save the complete dataset or DataTable.WriteXml to save contents of a single table.

    I'm not sure if this addresses your needs - if it doesn't then please give more (brief) information/code sample on:

    1. How does the XML file gets loaded into DataSet (via XmlDataDocument, or DataSet.ReadXml, etc)

    2. How does City value get modified.

    3. Why does the output if DataSet.WriteXml not meet the needs Is it that the output XML is in-valid as compared to the input XML.

    Thanks,

    Kawarjit Bedi

    Program Manager - ADO.NET

    kbedi@online.microsoft.com



  • Trevor Clark

    I missed saying how to retrieve the rows to be modfied in my previous posting and the following line may not actually compile.

    > myDataSet.Tables["ADDRESS"].Rows["CITY"]="newCityValue"

    instead you may want to try the following:

    DataRow[] rows = myDataSet.Tables["ADDRESS"].Select("City = 'OldCity'
    ");

    Iterate over the above rows and change rowsIdea["City"]= "new city" as needed.

    Thanks,

    Kawarjit Bedi



  • rhfritz

    Hello,

    I think I will simplify my question. Sorry about the long previous post

    Let say I have the following in my xml file :

    <MEMBER>
    <GENDER>FEMALE</GENDER>
    <AGE>35</AGE>
    - <ADDRESSES>
    - <ADDRESS>
    <ID>1</ID>
    <STREET>cranbrook ave</STREET>
    <POSTCODE>1231231</POSTCODE>
    <CITY>NOTTINGHAM</CITY>
    <COUNTRY>UK</COUNTRY>
    </ADDRESS>
    </ADDRESSES>
    </MEMBER>

    After displaying only the <ADDRESSES> field in dataset (datagrid) -extracted with xpath - and storing changes and updated <ADDRESS> to XmlDataDocument, how could I update my xml file and produce something like this :

    <MEMBER>
    <GENDER>FEMALE</GENDER>
    <AGE>35</AGE>
    - <ADDRESSES>
    - <ADDRESS>
    <ID>1</ID>
    <STREET>cranbrook ave</STREET>
    <POSTCODE>1231231</POSTCODE>
    <CITY>LONDON</CITY>
    <COUNTRY>UK</COUNTRY>
    </ADDRESS>
    </ADDRESSES>
    </MEMBER>

    I could not do dataset.WriteXml or xmldatadoc.save because the updated xml data document will not have <MEMBER> <GENDER> and <AGE> and I need these elements to stay.

    Any suggestions and help are greatly appreciated.

    Many thanks,

    Sharvie



  • How to enable DataGridView updates for xpath extract results