2009-02-23 36 views
11

Tôi đã sau file:truy vấn XmlNode sử dụng LINQ

<root> 
    <Product desc="Household"> 
    <Product1 desc="Cheap"> 
     <Producta desc="Cheap Item 1" category="Cooking" /> 
     <Productb desc="Cheap Item 2" category="Gardening" /> 
    </Product1> 
    <Product2 desc="Costly"> 
     <Producta desc="Costly Item 1" category="Decoration"/> 
     <Productb desc="Costly Item 2" category="Furnishing" /> 
     <Productc desc="Costly Item 3" category="Pool" /> 
    </Product2> 
    </Product> 
</root> 

Tôi muốn tìm hiểu thông tin như: Tổng số mục trong giá rẻ và đắt, danh sách của tất cả các loại (như nấu ăn, làm vườn, trang trí ...) , danh sách danh mục được sắp xếp và chỉ chọn Sản phẩm 'Chi phí'

Tôi có thể làm gì bằng cách sử dụng LINQ. Tôi đã làm điều này cho đến bây giờ:

XElement xe = XElement.Load(Server.MapPath("~/product.xml")); 
???? 

Trả lời

9

Cấu trúc XML của bạn không may khi sử dụng phần tử Sản phẩm cho ba cấp độ phân cấp. Bạn có các yếu tố khác tương tự như "hộ gia đình" không?

Giả sử chúng tôi chỉ muốn những người thân trong gia đình, bạn có thể sử dụng:

mục Đếm trong mỗi giá rẻ/tốn kém

xe.Element("Product") // Select the Product desc="household" element 
    .Elements() // Select the elements below it 
    .Select(element => new { Name=(string) element.Attribute("desc"), 
          Count=element.Elements().Count() }); 

Liệt kê tất cả các loại

xe.Descendants() // Select all descendant elements 
    .Attributes() // All attributes from all elements 
    // Limit it to "category" elements 
    .Where(attr => attr.Name == "category") 
    // Select the value 
    .Select(attr => attr.Value) 
    // Remove duplicates 
    .Distinct(); 

Để sắp xếp này, chỉ cần sử dụng .OrderBy(x => x) ở cuối.

Chọn 'tốn kém' sản phẩm

xe.Descendants() // Select all elements 
    // Only consider those with a "Costly" description 
    .Where(element => (string) element.Attribute("desc") == "Costly") 
    // Select the subelements of that element, and flatten the result 
    .SelectMany(element => element.Elements()); 
+1

tôi thực sự cần phải dạy cho bản thân mình LINQ ... âm thanh soooo dễ dàng theo cách này:) – balexandre

+0

Tôi đã thay đổi xml và nó đang hoạt động..thanks for answer –

+1

Tôi nghi ngờ Jon dự định bạn đổi tên các phần tử riêng lẻ * trong * dữ liệu; thay vì có tên khác nhau ở mỗi cấp * ... –

9

Vâng, cá nhân tôi thấy nó dễ dàng hơn với XmlDocument:

XmlDocument root = new XmlDocument(); 
    root.LoadXml(xml); // or .Load(path); 

    var categories = root.SelectNodes(
     "/root/Product/Product/Product/@category") 
     .Cast<XmlNode>().Select(cat => cat.InnerText).Distinct(); 
    var sortedCategories = categories.OrderBy(cat => cat); 
    foreach (var category in sortedCategories) 
    { 
     Console.WriteLine(category); 
    } 

    var totalItems = root.SelectNodes(
     "/root/Products/Product/Product").Count; 
    Console.WriteLine(totalItems); 

    foreach (XmlElement prod in root.SelectNodes(
     "/root/Product/Product[@desc='Costly']/Product")) 
    { 
     Console.WriteLine(prod.GetAttribute("desc")); 
    }