tags:

views:

57

answers:

2

Hi all,

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?

+3  A: 

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
                    };
Prashant
This is what I was thinking, unable to run the code so cannot verify.
Finglas
That worked - cheers.
Gogster
well, that still couldn't work with Element("name") and Attribute("email")....
Stephane
A: 

Changing Attribute("email") into Element("email") worked out just fine for me. tested, hopefully approved.

    public void XMLTEst()
    {
        var 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>Linda 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>";


        XElement xmlFile = XElement.Parse(xml);

        var query = from p in xmlFile.Descendants("member")
                    select new
                    {
                        Name = p.Element("name").Value,
                        Email = p.Element("email").Value
                    };


        foreach (var member in query)
        {
            Response.Write("Employee: " + member.Name + " " + member.Email + "<br />");
        }

    }
Stephane