XML遍历(LINQ TO XML的魅力)

有了LINQ,一切都变得这样容易,LINQ TO XML帮助我们封装了更多XML的操作,下面讲一下它的
遍历,其中用户到了结点,结点值,结点史称,结点属性等知识点。
category.xml文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<Categories>
  <Category>
    <Item title="true" class="knnn">
      考   研:
    </Item>
    <Item em="true" id="1111.2" src="http://www.b2cedu.com/UserInfos/MyStudyClass">
      MBA
    </Item>
    <Item em="true" id="1111.3" src="http://www.b2cedu.com/Orders">
      考博
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      普研
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      在职硕士
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      同等学力
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      GCT
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      法律硕士
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      会计硕士
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      公共管理硕士
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      教育硕士
    </Item>
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      财   贸:
    </Item>
    <Item em="true" id="1111.3" src="http://www.b2cedu.com/Orders">
      会计职称
    </Item>
    <Item em="true" id="1111.3" src="http://www.b2cedu.com/Orders">
      注册会计师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      注册税务师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      经济师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      高级会计师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      审计师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      报关员
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      报检员
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      会计证
    </Item>
 
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      工   程:
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      一级建造师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      二级建造师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      造价工程师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      建筑师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      监理工程师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      安全工程师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      咨询工程师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      房地产估价师
    </Item>
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      司   考:
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      司法考试
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      企业法律顾问
    </Item>
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      公 务 员:
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      国家公务员
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      地方公务员
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      教师资格考试
    </Item>
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      学   历:
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      成人高考
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      自学考试
    </Item>
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      外   语:
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      考博英语
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      日语
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      法语
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      韩语
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      德语
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      俄语
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      成人英语三级
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      职称英语
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      英语四六级
    </Item>
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      医   学:
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      执业药师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      卫生职称
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      中医医师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      口腔执业医师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      临床执业助理医师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      临床执业医师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      营养师
    </Item>
  </Category>
  <Category>
    <Item  title="true" class="knnn">
      职   业:
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      物业管理师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      项目管理师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      理财规划师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      心理咨询师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      人力资源管理师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      物流师
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      银行从业
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      证券资格
    </Item>
    <Item id="1111.3" src="http://www.b2cedu.com/Orders">
      特许金融分析师
    </Item>
  </Category>
</Categories>

下面是ASPX文件内容:

 

<div id="navigation">
                                    <%
                                        System.Xml.Linq.XElement root = System.Xml.Linq.XElement.Load(Server.MapPath("~/views/shared/category.xml"));
                                        root.Elements("Category").ToList().ForEach(item =>
                                        {%>
                                    <ul>
                                        <%
                                            item.Elements("Item").ToList().ForEach(i =>
                                           {
                                               if (i.Attribute("title") != null)
                                               {
                                        %>
                                        <li class='<%=i.Attribute("class").Value %>'>
                                            <%=i.Value %></li>
                                        <%}
                                               else
                                               { %>
                                        <li><a href="javascript:void(0)" onclick="CategoryRedirect('<%=i.Attribute("id").Value %>','<%= i.Attribute("src").Value%>')"
                                            title="<%=i.Value %>">
                                            <%if (i.Attribute("em") != null)
                                              {
                                            %><em><%=i.Value %></em><%}
                                              else
                                              { %>
                                            <%=i.Value %>
                                            <%} %>
                                        </a></li>
                                        <%} %>
                                        <%  
                                            });%>
                                    </ul>
                                    <%
                                        });
                                    %>
                                </div>

posted @ 2011-08-01 16:54  张占岭  阅读(655)  评论(0编辑  收藏  举报