
Untitled
By: a guest on
Jun 17th, 2012 | syntax:
None | size: 1.89 KB | hits: 19 | expires: Never
Linq to XML Queries
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer Name="Jason Voorhees" WeaponPurchased="Machette" SalePrice="499.90" />
<Customer Name="Michael Myers" WeaponPurchased="Kitchen Knife" SalePrice="96.75" />
</Customers>
foreach customer in Customers select WeaponPurchased where Name equals "Jason Voorhees"
foreach customer in Customers select customer
label1.Text += "Name: " + customer.Name + Environment.NewLine + "WeaponPurchased: " + customer.WeaponPurchased;
var doc = XDocument.Load(Path.Combine(path, "file.xml"));
var query = from c in doc.Descendants("Customer")
where c.Attributes("Name").Single().Value == "Jason Voorhees"
select c.Attributes("WeaponPurchased").Single().Value;
XElement root = XDocument.Load(path).Root;
var result = root.Elements("Customers").
Where(x => x.Attribute("Name").Value =="Jason Voorhees").
Select(x => x.Attribute("WeaponPurchased").Value));
public class Customer
{
public string Name { get; set; }
public string WeaponPurchased { get; set; }
public string SalePrice { get; set; }
}
var customer = from c in XDocument.Load("XMLPATH").Descendants("Customer")
where (string)c.Attribute("Name")=="Jason Voorhees"
select new Customer
{
Name=(string)c.Attribute("Name"),
WeaponPurchased = (string)c.Attribute("WeaponPurchased"),
SalePrice = (string)c.Attribute("SalePrice"),
};
foreach (var item in customer)
{
Console.WriteLine(item.WeaponPurchased);
}
from customer in Customers
where customer.Attribute("Name").Value equals "Jason Voorhees"
select customer.Attribute("WeaponPurchased").Value