2009-02-23 7 views
11

का उपयोग कर:क्वेरी XmlNode मैं फ़ाइल निम्न है 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> 

मैं तरह की जानकारी पता लगाना चाहते हैं: सस्ते और महंगा, सभी श्रेणी के सूची में कुल आइटम (पाक कला की तरह, बागवानी, सजावट ...) , क्रमबद्ध श्रेणी की सूची और केवल उत्पाद का चयन करें जो 'महंगा'

मैं LINQ का उपयोग कैसे कर सकता हूं। मैं अब तक ऐसा किया: के रूप में यह पदानुक्रम के तीन स्तरों के लिए एक उत्पाद तत्व का उपयोग करता

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

उत्तर

9

आपका XML संरचना दुर्भाग्यपूर्ण है। क्या आपके पास "घरेलू" के समान अन्य तत्व हैं?

मान लिया जाये कि हम केवल घर के लोगों को चाहते हैं, आप उपयोग कर सकते हैं:

गणना आइटम सस्ते में से प्रत्येक में/महंगा

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() }); 

सूची सभी श्रेणियों

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(); 

करने के लिए इसे सॉर्ट करें, अंत में .OrderBy(x => x) का उपयोग करें।

चुनें 'महंगा' उत्पादों

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

मैं वास्तव में अपने आप को LINQ सिखाने की आवश्यकता ... soooo आसान इस तरह से लग रहा है:) – balexandre

+0

मैंने एक्सएमएल बदल दिया है और यह काम कर रहा है .. उत्तर –

+1

के लिए धन्यवाद मुझे संदेह है कि जॉन का इरादा है कि आप अलग-अलग तत्वों का नाम बदलकर * डेटा के भीतर; प्रत्येक * स्तर * पर अलग-अलग नाम रखने के लिए ... –

9

ठीक है, व्यक्तिगत रूप से मैं इसे 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")); 
    }