当前位置:编程学习 > XML/UML >>

linq to xml读取属性的值

xml文件如下:<?xml version="1.0" encoding="UTF-8"?>
<record_info RecordIndex="1" Upload2ShareCenterTimes="1" Upload2ResShareCenter="1" Upload2ShareCenterStatus="2">
  <LessonGUID>22DE52E8-3AB6-42DE-9014-2E44B33A0E11</LessonGUID>
  <RecTime StartTime="2011-06-15 14:49:36" EndTime="2011-06-15 14:49:51"/>
  <CourseName>22</CourseName>
  <LessonName>11</LessonName>
  <SpeakerName>22</SpeakerName>
  <Introduction/>
  <ImageIndex>
    <Index Name="24008984.jpg" Time="00:00:00"/>
  </ImageIndex>
  <ChannelInfo Count="4">
    <Channel Name="教师桌面" Type="0" ID="0" Resolution="640 480" Quality="60" BandWidth="768" FrameRate="15" Keyframe="100" EncMode="VBR" Deinterlace="0" Denoise="0"/>
    <Channel Name="老师通道" Type="1" ID="1" Resolution="640 480" Quality="60" BandWidth="768" FrameRate="25" Keyframe="100" EncMode="CBR" Deinterlace="1" Denoise="1"/>
    <Channel Name="学生通道" Type="3" ID="3" Resolution="640 480" Quality="60" BandWidth="768" FrameRate="25" Keyframe="100" EncMode="VBR" Deinterlace="1" Denoise="1"/>
    <Channel Name="全景视频" Type="2" ID="2" Resolution="720 576" Quality="60" BandWidth="768" FrameRate="25" Keyframe="100" EncMode="VBR" Deinterlace="1" Denoise="1"/>
  </ChannelInfo>
  <VidInfo IsRecSM="1" IsRecMM="1" SMFormat="1" MMFormat="2" TotalTime="00:00:12">
    <Vid_SM SplitCount="4">
      <RecFile Name="20110615144936.iac" Format="1" Channel="-1"/>
    </Vid_SM>
    <Vid_MM VidSize="720 576">
      <RecFile Name="20110615144936_M.wmv" Format="2"/>
    </Vid_MM>
  </VidInfo>
</record_info>
要求读取<RecFile Name="20110615144936_M.wmv" Format="2"/>该节点中的Name,并要求Format=2
实现代码如下:
 public static string GetVideoFile(string url)
        {
            string videoFilename = null;
            //var ss = from e in XDocument.Load(url).Elements("record_info").Elements("Vid_MM").Elements("RecFile")
            var query = from c in XDocument.Load(url).Elements("record_info").Elements("VidInfo").Elements("Vid_MM").Elements("RecFile")
                        where (string)c.Attribute("Format").Value == "2"
                        select c;
            foreach (var book in query)
            {
               videoFilename=book.Attribute("Name").Value;
             
            }
            return videoFilename==null?null:videoFilename;
         

        }

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,