アットウィキロゴ

XML

<?xml version="1.0" encoding="utf-8"?>
<products>
 <product id="product1" category="book">
   <name>Programing Something</name>
   <price>2000</price>
   <publishDate>2008/10/10</publishDate>
   <authors>
       <author>Tarou Book</author>
       <author>Hanako Book</author>
   </authors>
 </product>
 <product id="product2" category="book">
   <name>Administrating Something</name>
   <price>5000</price>
   <publishDate>2008/10/12</publishDate>
   <authors>
       <author>Kotarou Book1</author>
       <author>Kohanako Book2</author>
   </authors>
 </product>
 <product id="product3" category="novel">
   <name>Suspection novel</name>
   <price>500</price>
   <publishDate>2007/12/12</publishDate>
   <authors>
       <author>Jirou Tarou</author>
   </authors>
 </product>
 <product id="product4" category="novel">
   <name>Fantasy novel</name>
   <price>540</price>
   <publishDate>2008/9/14</publishDate>
   <authors>
       <author>Tarou Book</author>
   </authors>
 </product>
 <product id="product5" category="cram">
   <name>Study English</name>
   <price>2400</price>
   <publishDate>2008/9/14</publishDate>
   <authors>
       <author>English Tarou</author>
       <author>English Jirou</author>
   </authors>
 </product>
</products>
XElement products = XElement.Load(@"D:\temp\products.xml");

var query = from p in products.Elements("product")
           where (string)p.Attribute("category") == "book"
           select new
           {
               ID = (string)p.Attribute("id"),
               Name = (string)p.Element("name"),
               Price = (decimal)p.Element("price")
           };
foreach (var item in query)
{
   Console.WriteLine(item);
}
           var xeml = XElement.Load(@"C:\Users\kanno\Desktop\test.xml");
           IEnumerable<XElement> test = xeml.Elements("product");
           var t = test.Select((value, index) => new { value, index });
           foreach(var s in t){
               Console.WriteLine(s.index);   
           }
最終更新:2015年01月28日 23:49