lets say i got some xml file which looks like this:
<NewDataSet>
<Table1 id = "one">
<Server>Server1</Server>
<Database>Database1</Database>
</Table1>
<Table1 id = "two">
<Server>Server2</Server>
<Database>Database2</Database>
</Table1>
</NewDataSet>
what i want to do is that i want to read the values from this xml and put them in datagridview
, in different columns. for ex: to show Database1
and Database2
in one column. im using Xpath to get values from xml file, here is my code:
private void xmlGetValues()
{
XPathDocument doc = new XPathDocument("C:\\XMLfile1.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile("/NewDataSet/Table1/Database");
XPathNodeIterator iterator = nav.Select(expr);
try
{
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
this will get me all the values from Database
tags and i cant figure it out how to give those values to datagridviewtextbox
to show in my datagridview
. Can anyone help me with this? (PS sorry for my bad English)
Try,
XElement root = XElement.Load(file);
var tables = root.Descendants("Table1")
.Select(t => new
{
Server = t.Element("Server").Value,
Database = t.Element("Database").Value
});
foreach(var table in tables)
grid.Rows.Add(new object[] { table.Server, table.Database });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.