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>

posted @ 2008-12-26 11:46  昕友软件开发  阅读(1118)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯