-5

我有几个以下格式的 xml 文件。

<!--?xml version="1.0" standalone="yes"?-->
<details>
  <Classes>
    <class>5</class>
    <section>A</class>
  </Classes>
  <student>
    <firstname>Avijeet</firstname>
    <lastname>Roy</lastname>
    <roll>01</roll>
  </student>
  <student>
    <firstname>Saurav</firstname>
    <lastname>Das</lastname>
    <roll>02</roll>
  </student>
   <Classes>
    <class>10</class>
    <section>A</class>
  </Classes>
  <student>
    <firstname>Saumitra</firstname>
    <lastname>Bain</lastname>
    <roll>10</roll>
  </student>
  <student>
    <firstname>Tarun</firstname>
    <lastname>Sing</lastname>
    <roll>11</roll>
  </student>
</details>

现在我想要一个像我的 asp.net 页面一样的数据网格

Class   Section fname    lname  roll
5   A   Avijeet  Roy    01
5   A   Saurav   Das    02
10  A   Saumitra Bain   10
10  A   Tarun    Sing   11

我搜索了很多,但找不到类似的东西。我是 C# 新手

4

2 回答 2

6

我认为您拥有的 xml 格式不符合您的要求,应该是这样的:

<details>
 <Classes>
      <Class>
        <class>5</class>
        <section>A</class>
        <student>
         <firstname>Avijeet</firstname>
         <lastname>Roy</lastname>
         <roll>01</roll>
       </student>
       <student>
        <firstname>Saurav</firstname>
        <lastname>Das</lastname>
        <roll>02</roll>
      </student>
      </Class>

       <Class>
        <class>10</class>
        <section>A</class>
        <student>
         <firstname>Saumitra</firstname>
         <lastname>Bain</lastname>
         <roll>10</roll>
       </student>
       <student>
        <firstname>Tarun</firstname>
        <lastname>Sing</lastname>
        <roll>11</roll>
      </student>
      </Class>
  </Classes> 
</details>

在此之后,您可以使用 LINQ to XML:

例如。:

XDocument doc = XDocument.Load(xmlPath);
IEnumerable<XElement> selectors = from elements in doc.Elements("details").Elements("Classes").Elements("Class")
                                  select elements;
于 2012-08-23T11:53:50.293 回答
0

您可以尝试创建反映 XML 文件结构的类型化数据集并从 XML 文件加载数据。然后你可以做数据绑定。此外,您可以使用Linq to XML

于 2012-08-23T11:50:23.447 回答