Linq to XML的练习

  假如有以下XML:

<?xml version="1.0" encoding="utf-8" ?>
-   <export>
-     <packages>
-       <package id="111670618065528135">
          <path>D:\888\</path>
-         <files>
-           <file id="111670623796596151">
              <path>\韩寒_要自由.htm</path>
              <category>民主</category>
          </file>
-           <file id="111670623796594149">
            <path>\谈谈韩寒三论.htm</path>
            <category>民主\中国</category>
          </file>
-           <file id="111670623796595150">
            <path>\韩寒_我的2011.htm</path>
            <category>民主\中国</category>
           </file>
-           <file id="111670623796596151">
            <path>\韩寒_要自由.htm</path>
            <category>民主\中国</category>
          </file>
-           <file id="111670623796597152">
            <path>\韩寒_说民主.htm</path>
            <category>民主\中国</category>
          </file>
-           <file id="111670623796599153">
            <path>\韩寒_谈革命.htm</path>
            <category>民主\中国</category>
          </file>
        </files>
        <files>
-           <file id="111670623796596151">
              <path>\韩寒_要自由.htm</path>
              <category>民主</category>
          </file>
-           <file id="111670623796594149">
            <path>\谈谈韩寒三论.htm</path>
            <category>民主\中国</category>
          </file>
-           <file id="111670623796595150">
            <path>\韩寒_我的2011.htm</path>
            <category>民主\中国</category>
           </file>
          </files>
        </package>
       <package id="111670618065528135">
          <path>D:\888\</path>
-         <files>
-           <file id="111670623796596151">
              <path>\韩寒_要自由.htm</path>
              <category>民主</category>
          </file>
-           <file id="111670623796594149">
            <path>\谈谈韩寒三论.htm</path>
            <category>民主\中国</category>
          </file>
        </files>
      </package>
      </packages>
    </export>
 
可采用以下方式进行读取
 
 class Program
    {
        public static XElement InitializeXMLDate()
        {
            string XMLPath = @"D:\附件1 - 副本.xml";
            if(!File.Exists(XMLPath))
 
            {
                throw new FileNotFoundException("The XML data file is missing");
            }
 
            XElement data = XElement.Parse(File.ReadAllText(XMLPath));
            return data;
        }
 
        static void Main(string[] args)
        {
 
            var packages = InitializeXMLDate().Elements().Where(element => element.Name.ToString() == "packages");
            foreach (var package in packages.Elements())
            {
                Console.WriteLine(package.Attribute("id").Value);
                Console.WriteLine(package.Element("path").Value);
               // var files = package.Elements().Where(element => element.Name.ToString()=="file");
                foreach (var file in package.Elements().Where(element => element.Name.ToString()=="files"))
                {
                    foreach (var property in file.Elements().Where(element => element.Name.ToString() == "file"))
                    {
                        Console.WriteLine(string.Format("{0}--------{1}"property.Element("path").Valueproperty.Element("category").Value));
                    }
                    Console.WriteLine("****");
                }
                Console.WriteLine("-----------------------------------");
            }
 
 
            Console.ReadKey();
        }
    }
 
posted @ 2013-10-15 14:36  蚂蚁拉车  阅读(188)  评论(0编辑  收藏  举报