LINQ to XML

  • Thread starter Thread starter André Freitas
  • Start date Start date
A

André Freitas

XDocument xDocument = XDocument.Load(MapPath("Breadcrumb.xml"));
var linq =
from breads in xDocument.Root.Elements("bread")
where breads.Attribute("script").Value == "/SomeScript.aspx"
select new
{
description =
(string)breads.Element("link").Attribute("description"),
url = (string)breads.Element("link").Attribute("url")
};

foreach (var linqRow in linq)
{
Response.Write("<script>alert('" + linqRow.description +
"')</script>");
}

<breadcrumb>
<bread script="/SomeScript.aspx">
<link description="some descriptiuon for acutal page"/>
<link description="Home" url="Default.aspx"/>
</bread>
...
</breadcrumb>

Its returning me only the first "link", not all the links inside bread.

Can someone point me how to solve it?

All the best,
André
 
André Freitas said:
XDocument xDocument = XDocument.Load(MapPath("Breadcrumb.xml"));
var linq =
from breads in xDocument.Root.Elements("bread")
where breads.Attribute("script").Value == "/SomeScript.aspx"
select new
{
description =
(string)breads.Element("link").Attribute("description"),
url = (string)breads.Element("link").Attribute("url")
};

foreach (var linqRow in linq)
{
Response.Write("<script>alert('" + linqRow.description +
"')</script>");
}

<breadcrumb>
<bread script="/SomeScript.aspx">
<link description="some descriptiuon for acutal page"/>
<link description="Home" url="Default.aspx"/>
</bread>
...
</breadcrumb>

Its returning me only the first "link", not all the links inside bread.

Can someone point me how to solve it?

All the best,
André

Got it:

XDocument xDocument = XDocument.Load(MapPath("Breadcrumb.xml"));
var linq =

from breads in xDocument.Root.Elements("bread")
where breads.Attribute("script").Value ==
Request.ServerVariables["SCRIPT_NAME"]
select new
{
links = from links in breads.Elements("link")
select new
{

description = (string)links.Attribute("description"),
url = (string)links.Attribute("url")
}
};


foreach (var bread in linq)
{
foreach (var link in bread.links)
{
Response.Write("<script>alert('" + link.description + "')</script>");
}
}
 
André Freitas said:
XDocument xDocument = XDocument.Load(MapPath("Breadcrumb.xml"));
var linq =
from breads in xDocument.Root.Elements("bread")
where breads.Attribute("script").Value == "/SomeScript.aspx"
select new
{
description =
(string)breads.Element("link").Attribute("description"),
url = (string)breads.Element("link").Attribute("url")
};

foreach (var linqRow in linq)
{
Response.Write("<script>alert('" + linqRow.description +
"')</script>");
}

<breadcrumb>
<bread script="/SomeScript.aspx">
<link description="some descriptiuon for acutal page"/>
<link description="Home" url="Default.aspx"/>
</bread>
...
</breadcrumb>

Its returning me only the first "link", not all the links inside bread.

Can someone point me how to solve it?

var linq =
from bread in xDocument.Root.Elements("bread")
where bread.Attribute("script").Value == "/SomeScript.aspx"
from link in bread.Elements("link")
select new
{
description =
(string)link.Attribute("description"),
url = (string)link.Attribute("url")
};
 
Martin Honnen said:
var linq =
from bread in xDocument.Root.Elements("bread")
where bread.Attribute("script").Value == "/SomeScript.aspx"
from link in bread.Elements("link")
select new
{
description =
(string)link.Attribute("description"),
url = (string)link.Attribute("url")
};

A lot better. Thankx.
 
Back
Top