XML, LINQ and Server.MathPath

S

shapper

Hello,

I having the following code to get some values from an XML file using
LINQ:

public List<string> GetDistricts() {
XDocument assets = XDocument.Load(Server.MapPath("Data/
Assets.xml"));
var districts = (from d in assets.Descendants("district")
select d).ToList();
return districts;
}

I am getting two errors:

1. The name 'Server' does not exist in the current context.
(I can't find the namespace where Server.MapPath is ... isn't
System.Web?)

2. Cannot implicitly convert type
'System.Collections.Generic.List<System.Xml.Linq.XElement>' to
'System.Collections.Generic.List<string>'

All I want is to create a list of string with the districts. And my
XML is something as follows:

<districts>
<district>First District</district>
<district>Second District</district>
</districts>

How can I solve these two problems?

Thanks,
Miguel
 
M

Martin Honnen

shapper wrote:

2. Cannot implicitly convert type
'System.Collections.Generic.List<System.Xml.Linq.XElement>' to
'System.Collections.Generic.List<string>'

Use

var districts = (from d in assets.Descendants("district")
select d.Value).ToList();
 
M

Martin Honnen

shapper said:
XDocument assets = XDocument.Load(Server.MapPath("Data/
Assets.xml"));

I am getting two errors:

1. The name 'Server' does not exist in the current context.
(I can't find the namespace where Server.MapPath is ... isn't
System.Web?)

Server is a member of Page, the ASP.NET page object. It does not make
much sense to use MapPath outside of ASP.NET? Is your code an ASP.NET
application?
 
S

shapper

Server is a member of Page, the ASP.NET page object. It does not make
much sense to use MapPath outside of ASP.NET? Is your code an ASP.NET
application?

Hi,

My code is in an ASP.NET MVC Preview 4 Application ... Not the usual
ASP.NET Web Forms.

Can't I use Server.MapPath in it?
If not, what should I use?

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
My code is in an ASP.NET MVC Preview 4 Application ... Not the usual
ASP.NET Web Forms.

Can't I use Server.MapPath in it?
If not, what should I use?

I don't know ASP.NET MVC, perhaps someone in an ASP.NET newsgroup knows
that stuff. Hmm, this forum http://forums.asp.net/t/1293114.aspx seems
to give you an answer Request.MapPath("yourpath") so try that.
 

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

Similar Threads


Top