LINQ to XML

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é
 
A

André Freitas

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>");
}
}
 
M

Martin Honnen

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")
};
 
A

André Freitas

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top