• 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();
            }
        }
     
  • 相关阅读:
    vue.js初识(一)
    node.js安装
    array_unshift
    查看php 某个服务的进程数
    获取src 内容
    微信支付 composer 方法 --- 实测有效
    tp5.1 model 方法下的like语句查询
    tp5.1 where 时间查询
    nginx conf 文件
    怎么用Ubuntu系统制作Ubuntu系统盘
  • 原文地址:https://www.cnblogs.com/xuekai-to-sharp/p/3370217.html
Copyright © 2020-2023  润新知