zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    爱奇艺首页导航轮播特效
    闭包的应用
    时间日期Date类型
    php连接数据库
    封装cookie.js、EventUtil.js、
    时间日期格式
    mysql数据库修改密码
    jquery选择器的使用
    用js完成blog项目
    Hibternate框架笔记
  • 原文地址:https://www.cnblogs.com/starcrm/p/1362845.html
Copyright © 2011-2022 走看看