zoukankan      html  css  js  c++  java
  • 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();
            }
        }
     
  • 相关阅读:
    firefox浏览器播放音频
    Font Awesome图标字体应用及相关
    PHP输出A到Z及相关
    TensorFlow安装填坑之路(Windows环境)
    Git常用命令(一)
    spring boot 入门(一)
    JHipster简介
    Spring Boot实现文件下载功能
    IntelliJ IDEA插件系列
    什么是RESTful API?
  • 原文地址:https://www.cnblogs.com/xuekai-to-sharp/p/3370217.html
Copyright © 2011-2022 走看看