Reputation: 520
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.
Upvotes: 2
Views: 3758
Reputation: 63956
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.
Upvotes: 0
Reputation: 3389
If you are using Windows Forms Application, you should do the next thing:
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:
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.
Upvotes: 2