2010-06-21 92 views
0

这是我的XML文件。使用LinQ读取XML文件

<?xml version="1.0" encoding="utf-8"?> 
<locstrings> 
<section name="section1"> 
    <locstring ID="sectionID1"> 
     <Name>SectionName1</Name> 
    </locstring> 
</section> 
<section name="section2"> 
    <locstring ID="sectionID2"> 
     <Name>SectionName2</Name> 
    </locstring> 
    <locstring ID="SectionID3"> 
     <Name>SectionName3</Name> 
    </locstring> 
</section> 

</locstrings> 
  1. 我想读这个XML元素和绑定(仅部分姓名),DataGrid1中使用LINQ在C#。
  2. 基于DataGrid1行选择,我想显示sectionID和sectionName在dataGrid2使用LINQ。
+0

你们又想出什么有那么远,发布的[阅读使用XML文件的LinQ(HTTP一些代码 – 2010-06-21 15:48:15

+0

可能重复:/ /stackoverflow.com/questions/3085644/read-xml-file-using-linq) – 2010-08-04 11:07:00

回答

0

下面是一些示例的LINQ to XML来从示例XML文件中的数据:

 XDocument xml = XDocument.Load(@"<path to your xml file"); 

     var resultSet = from x in xml.Descendants("section")       
         select x.Attribute("name"); 

     var resultsSet2 = from x in xml.Descendants("section") 
          where x.Attribute("name") == "<the selected value of your data grid>" 
          select new 
          {        
           id = x.Element("locstring").Attribute("ID").Value, 
           name = x.Element("locstring").Element("Name").Value 
          }; 

您需要设置您的数据网格,然后绑定使用.DataSource属性的结果集,然后调用数据网格上的.DataBind()方法。

您还需要在第一个DataGrid上设置SelectedIndexChanged事件处理程序来捕获选定的值,并使用LINQ to XML代码为resultSet2获取第二个网格结果集。

DataGrid绑定和选择有很好的文档记录。但在这里是如何做到这一点一些MSDN文档:

Data Binding to a DataGrid Control