2

I have been looking for an answer for this for a while: I have an XML file that is formatted like so:

<root>
    <string id = "STRING_ID">
        <node1> Some data </node1>
        <node2>
            <type>data</type>
        </node2>
        <Translations>
           <language name="ARABIC">
               <value>Some data</value>
               <date_last_changed>7-4-2011</date_last_changed>
           </language>
           <language name="CHINESE">
            ...
            ...
        </Translations>
    </string>

    <string id = "...">
       ...
       ...
</root>

I loaded a DataSet with information from an XML file. I then bound the DataSet to a DataGridView via

DataGridView1.DataSource = dataSet1;

and then used

DataGridView1.DataMember = "string";

Currently the datagridview displays the content in node 1, and the value of the id attribute of each string element.

My question is: How do I populate a column with the data contained in all of the <value> elements of <language> in each <string> whose language name = "ENGLISH" or "ARABIC" and so on? Sounds like a query to me, but I'm not sure how to do this.

Basically I want one column to always display every english string corresponding to the appropriate string ID in my datagridview, and then one more column to display every string of a language that I select using a list in a ComboBox.

Sean Glover
  • 520
  • 6
  • 22

2 Answers2

2

If you are using Windows Forms Application, you should do the next thing:

  1. Drag and drop DataGridView for defined table from DataSource onto Form. That way the BindingNavigator and BindingSource are auto defined.
  2. Then you can edit DataGridView with a click on the arrow (look on the Form), which opens DataGridView Tasks. Here you can edit and add columns and do lots of other stuff (preview data etc.).

If the DataGridView would not show you appropriate columns, then check under DataGridView properties, to which DataSource is currently bind. It has to be to properly defined, if you want to manipulate columns.

Here is an example of DataSource, that can then be dragged and dropped on Windows Form Application. As you can see, the attributes can be dropped as various Controls. In your example, you should set the language as ComboBox. In this example, you can see the attribute Oznaka under table Model defined as ComboBox:
Example of properly defined DataSet.

In the end, it all depends, how the DataSet is constructed from the XML (which attributes are available to you). So if you do not have the right attributes defined for your problem, than you should go back and design proper DataSet.

Jernej Jerin
  • 3,179
  • 9
  • 37
  • 53
  • Hi Jernej, thank you for the reply. I am unsure how to define my dataset as a data source though. I have a dataset defined using the designer view of my Windows Form Application, and then I define it as the DataSource for my DataGridView in the code. Any ideas? – Sean Glover Jul 27 '11 at 20:05
  • You probably added empty DataSet to project with Add -> New Item -> DataSet and then programmatically filled it with xml file? If so, I think you should create an xml scheme (.xsd file) from your XML (there are a lot of links on net how to do that) and then use .xsd file to create DataSet. That way you will see under Data Sources DataSet, that has defined tables and attributes according to your xsd (or xml). – Jernej Jerin Jul 27 '11 at 21:00
  • Oh okay. Actually I had already written an XSD prior to this for future use. I also have the schema already added to my project. – Sean Glover Jul 27 '11 at 21:08
0

Sean, how about something like this:

XDocument xdoc = XDocument.Load("<path_to_your_xml_file>");
        var myLittleObjects = (from y in
                                   (from c in xdoc.Descendants("string")
                                    select c).ToList()

                               select new
                               {
                                   Nodes = (from p in
                                                (from h in y.Descendants()
                                                 where h.Name.LocalName.Contains("node")
                                                 select h).ToList()
                                            select new { NodeValue = p.Value, NodeType = (from t in p.Descendants("type").ToList() select t.Value).ToList() }).ToList(),

                                    //Translations = ...  //same logic as for nodes

                               }

                               ).ToList();


        Console.WriteLine(myLittleObjects.Count());

Now you have Business Objects to work with. This will allow you bind the specific object properties of the "myLittleObjects" variable to your data grid.

Note that just that simple statement will transform the whole XML file into a list of objects which in turn will have a "Nodes" and a "Translations" Property and those properties will in turn be their own classes with their respective properties (in the case of Node will be NodeValue and NodeType which in turn is a List)

I am sorry for my sloppy writing.

Icarus
  • 63,293
  • 14
  • 100
  • 115