I have the following code:
protected void Page_Load(object sender, EventArgs e)
{
XElement xml = XElement.Load(Server.MapPath("ArenasMembers.xml"));
var query = from p in xml.Descendants("members")
select new
{
Name = p.Element("name").Value,
Email = p.Attribute("email").Value
};
foreach (var member in query)
{
Response.Write("Employee: " + member.Name + " " + member.Email + "<br />");
}
}
Which, using the hover information in Visual Studio, is reading the XNL file in correctly, however the foreach is not outputting any of the records.
XML:
<?xml version="1.0" encoding="utf-8" ?>
<members>
<member>
<arena>EAA Office</arena>
<memberid>1</memberid>
<name>Jane Doe</name>
<email>[email protected]</email>
</member>
<member>
<arena>EAA Office</arena>
<memberid>2</memberid>
<name>John Bull</name>
<email>[email protected]</email>
</member>
<member>
<arena>O2 Arena</arena>
<memberid>3</memberid>
<name>John Doe</name>
<email>[email protected]</email>
</member>
<member>
<arena>O2 Arena</arena>
<memberid>4</memberid>
<name>Bernard Cribbins</name>
<email>[email protected]</email>
</member>
<member>
<arena>Colourline Arena</arena>
<memberid>5</memberid>
<name>John Bon Jovi</name>
<email>[email protected]</email>
</member>
<member>
<arena>NIA</arena>
<memberid>6</memberid>
<name>Rhianna</name>
<email>[email protected]</email>
</member>
</members>
Can you see what is wrong?
Insted of "members" try "member"...
Descendants returns you all the child elements under given Element, in your code you are querying members with name and email, which wont work.
Sample Code:
var query = from p in xml.Descendants("member")
select new
{
Name = p.Element("name").Value,
Email = p.Element("email").Value
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With