• Linq to xml:检索


    //检索元素集合           

    XElement po = XElement.Load("c:/test.xml");
                IEnumerable<XElement> childElements =
                    //from el in po.Descendants("Book")
                    from el in po.Descendants("Title")
                    select el;
                foreach (XElement el in childElements)
                    Console.WriteLine("Name: " + (string)el);


                Console.ReadLine();

    <?xml version="1.0" encoding="utf-8"?>
    <!--This is a comment.-->
    <?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>
    <Pubs>
      <Book>
        <Title>Artifacts of Roman Civilization</Title>
        <Author>Moreno, Jordao</Author>
      </Book>
      <Book>
        <Title>Midieval Tools and Implements</Title>
        <Author>Gazit, Inbar</Author>
      </Book>
    </Pubs>
    <!--This is another comment.-->

                查找具有值为“Billing”的 Type 属性的 Address 元素
                XElement root = XElement.Load("c:/PurchaseOrder.xml");
                IEnumerable<XElement> address =
                    from el in root.Elements("Address")
                    where (string)el.Attribute("Type") == "Billing"
                    select el;
                foreach (XElement el in address)
                    Console.WriteLine(el);

                查找 Test 元素,该元素包含具有值为“Examp2.EXE”的 CommandLine 子元素
                XElement root = XElement.Load("TestConfig.xml");
                IEnumerable<XElement> tests =
                    from el in root.Elements("Test")
                    where (string)el.Element("CommandLine") == "Examp2.EXE"
                    select el;
                foreach (XElement el in tests)
                    Console.WriteLine((string)el.Attribute("TestId"));

                查询树根部的子元素
                            File.WriteAllText("Test.xml", @"<Root>
                                                                        <Child1>1</Child1>
                                                                        <Child2>2</Child2>
                                                                        <Child3>3</Child3>
                                                                        </Root>");

                            Console.WriteLine("Querying tree loaded with XElement.Load");
                            Console.WriteLine("----");
                            XElement doc = XElement.Load("Test.xml");
                            IEnumerable<XElement> childList =
                                from el in doc.Elements()
                                select el;
                            foreach (XElement e in childList)
                                Console.WriteLine(e);

                具有 Type 属性等于“Shipping”的子 Address 元素和等于“NY”的子 State 元素的所有 PurchaseOrder 元素
                XElement root = XElement.Load("PurchaseOrders.xml");
                IEnumerable<XElement> purchaseOrders =
                    from el in root.Elements("PurchaseOrder")
                    where
                        (from add in el.Elements("Address")
                         where
                             (string)add.Attribute("Type") == "Shipping" &&
                             (string)add.Element("State") == "NY"
                         select add)
                        .Any()
                    select el;
                foreach (XElement el in purchaseOrders)
                    Console.WriteLine((string)el.Attribute("PurchaseOrderNumber"));

                XElement root = XElement.Parse(@"<Root>
                                                  <Child1>
                                                    <Text>Child One Text</Text>
                                                    <Type Value=""Yes""/>
                                                  </Child1>
                                                  <Child2>
                                                    <Text>Child Two Text</Text>
                                                    <Type Value=""Yes""/>
                                                  </Child2>
                                                  <Child3>
                                                    <Text>Child Three Text</Text>
                                                    <Type Value=""No""/>
                                                  </Child3>
                                                  <Child4>
                                                    <Text>Child Four Text</Text>
                                                    <Type Value=""Yes""/>
                                                  </Child4>
                                                  <Child5>
                                                    <Text>Child Five Text</Text>
                                                  </Child5>
                                                </Root>");
                var cList =
                    from typeElement in root.Elements().Elements("Type")
                    where (string)typeElement.Attribute("Value") == "Yes"
                    select (string)typeElement.Parent.Element("Text");
                foreach (string str in cList)
                    Console.WriteLine(str);

    <?xml version="1.0"?>
    <PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
      <Address Type="Shipping">
        <Name>Ellen Adams</Name>
        <Street>123 Maple Street</Street>
        <City>Mill Valley</City>
        <State>CA</State>
        <Zip>10999</Zip>
        <Country>USA</Country>
      </Address>
      <Address Type="Billing">
        <Name>Tai Yee</Name>
        <Street>8 Oak Avenue</Street>
        <City>Old Town</City>
        <State>PA</State>
        <Zip>95819</Zip>
        <Country>USA</Country>
      </Address>
      <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>
      <Items>
        <Item PartNumber="872-AA">
          <ProductName>Lawnmower</ProductName>
          <Quantity>1</Quantity>
          <USPrice>148.95</USPrice>
          <Comment>Confirm this is electric</Comment>
        </Item>
        <Item PartNumber="926-AA">
          <ProductName>Baby Monitor</ProductName>
          <Quantity>2</Quantity>
          <USPrice>39.98</USPrice>
          <ShipDate>1999-05-21</ShipDate>
        </Item>
      </Items>
    </PurchaseOrder>

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    Django高级编程之自定义Field实现多语言
    Python魔法方法__getattr__和__getattribute__详解
    Python深入浅出property特性属性
    Python中使用__new__实现单例模式并解析
    Python中类方法、__new__方法和__init__方法解析
    Python中可迭代对象、迭代器以及iter()函数的两个用法详解
    Docker ubuntu apt-get更换国内源解决Dockerfile构建速度过慢
    Python抽象基类中__subclasshook__方法的使用并实现自己的虚拟子类
    Scrapy-redis分布式爬虫爬取豆瓣电影详情页
    PHP5.5+ APC 安装
  • 原文地址:https://www.cnblogs.com/starcrm/p/1362845.html
Copyright © 2020-2023  润新知