I am fallowing a tutorial on the internet I use xml file this .I think missed out something .Because Nothing happens Just a blank page appears.What I am doing wrong again.How can i bind filtered data to gridview
protected void Page_Load(object sender, EventArgs e)
{
XElement root = XElement.Load(Server.MapPath("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)
{
GridView1.DataSource = el;
}
GridView1.DataBind();
}
You're currently looking for elements in the unnamed namespace - whereas the file you've linked to has a namespace URI of "http://www.adventure-works.com"
for the elements you're looking for.
Just use:
XNamespace aw = "http://www.adventure-works.com";
IEnumerable<XElement> purchaseOrders =
from el in root.Elements(aw + "PurchaseOrder")
where
(from add in el.Elements(aw + "Address")
where
(string)add.Attribute("Type") == "Shipping" &&
(string)add.Element(aw + "State") == "NY"
select add)
.Any()
select el;
Or slightly more simply, IMO:
XNamespace aw = "http://www.adventure-works.com";
IEnumerable<XElement> purchaseOrders =
root.Elements(aw + "PurchaseOrder")
.Where(order => order.Elements(aw + "Address")
.Any(add => (string) add.Attribute(aw + "Type") == "Shipping"
&& (string) add.Element(aw + "State") == "NY"));
(Basically this doesn't use query expressions because they're not actually helping you here, and I'm using the Any
overload which takes a predicate...)
See more on this question at Stackoverflow