Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get the wards out of the xml file where the municipality has been selected:

xml:

XML
<wards>
  <wardid>0</wardid>
  <municipality>INGQUZA</municipality>
  <ward>04</ward>
  <wardid>1</wardid>
  <municipality>INGQUZA</municipality>
  <ward>04</ward>
  <wardid>2</wardid>
  <municipality>INGQUZA</municipality>
  <ward>05</ward>
  <wardid>3</wardid>
  <municipality>INGQUZA</municipality>
  <ward>07</ward>
</wards>



The code:

C#
string docLoc = HttpContext.Current.Request.MapPath("~/xml/wards.xml");
var doc=XDocument.Load(docLoc);
var el=
  from wards in doc.Element("wards").Elements("WARD")
  where wards.Elements("MUNICIPALITY") == DropDownList3.Text
  select wards;
foreach (string s in el) {
  DropDownList4.Items.Add(s);
}


If I leave out the where clause, it returns all the wards, how do I get it to return the wards where the municipality is selected in the dropdowlist?
Posted
Updated 18-May-10 8:18am
v2

You need to evaluate the value of the element, not the element itself

var el =
   from wards in doc.Elements("wards").SelectMany(e => e.Elements("municipality"))
   where wards.Value == DropDownList3.Text
   select wards.Parent;


Keep in mind xml is case sensitive so MUNICIPALITY is different than municipality
 
Share this answer
 
v2
Comments
dhjoubert@gmail.com 18-May-10 12:56pm    
Thanks for the reply, but it doesn't return any values, any ideas? I'm just starting with linq and xml:

string docLoc = HttpContext.Current.Request.MapPath("~/xml/wards.xml");
var doc=XDocument.Load(docLoc);
var el=
from wards in doc.Element("wards").Elements("WARD")
where wards.Element("MUNICIPALITY").Value == DropDownList3.Text
select wards;
foreach (string s in el) {
DropDownList4.Items.Add(s);
}
dhjoubert@gmail.com 19-May-10 1:28am    
Please help, it is driving me nuts. It still doesn't work, I can't believe it is so complicated. In SQL it would be:
select WARD from WARDS where MUNICIPALITY = 'INGQUZA'

The code that you gave me, returns the first record in the xml file, many times over and all on one line. I got it to return just the WARD, but it still returns only the first record many times over, which in this case is "04" repeated.
var el =
from wards in doc.Elements("wards").SelectMany(e => e.Elements("municipality"))
where wards.Value == DropDownList3.Text
select wards.Parent;
dhjoubert@gmail.com 19-May-10 2:19am    
I think I found one of the problems, the xml was not formed well, I have changed it, but the linq still eludes me:


<wards>
<wardsrec wardid="0"
="" municipality="INGQUZA" ward="04">

<wardsrec wardid="1"
="" municipality="INGQUZA" ward="04">

<wardsrec wardid="7"
="" municipality="KING SABATA DALINDYEBO" ward="01">

<wardsrec wardid="8"
="" municipality="KING SABATA DALINDYEBO" ward="02">

<wardsrec wardid="9"
="" municipality="KING SABATA DALINDYEBO" ward="03">

<wardsrec wardid="10"
="" municipality="KING SABATA DALINDYEBO" ward="04">

<?xml version="1.0" encoding="utf-8"?>
<wards>
<wardsrec WARDID="0"
MUNICIPALITY="INGQUZA"
WARD="04">
</wardsrec>
<wardsrec WARDID="1"
MUNICIPALITY="INGQUZA"
WARD="04">
</wardsrec>
<wardsrec WARDID="7"
MUNICIPALITY="KING SABATA DALINDYEBO"
WARD="01">
</wardsrec>
<wardsrec WARDID="8"
MUNICIPALITY="KING SABATA DALINDYEBO"
WARD="02">
</wardsrec>
<wardsrec WARDID="9"
MUNICIPALITY="KING SABATA DALINDYEBO"
WARD="03">
</wardsrec>
<wardsrec WARDID="10"
MUNICIPALITY="KING SABATA DALINDYEBO"
WARD="04">
</wardsrec>
</wards>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900