Gab
Gab

Reputation: 35

Only getting the first value while parsing an XML document using Linq

My XML file:

<tracking>
<tracking>
    <course id="1" name="AGL" nbrecredits="3">
        <instructor id="1" name="youssef">
            <session date="10-10-2012" numberOfsessions="1"/>
            <session date="9-10-2012" numberOfsessions="2"/>
            <session date="8-10-2012" numberOfsessions="1"/>
            <session date="7-10-2012" numberOfsessions="1.5"/>
        </instructor>
        <instructor id="2" name="Maroun"/>
            <session date="10-10-2012" numberOfsessions="4"/>
            <session date="9-10-2012" numberOfsessions="1"/>
    </course>
    <course id="2" name="projet de programmationa avancee" nbrecredits="3">
        <instructor id="1" name="youssef">
            <session date="10-10-2012" numberOfsessions="1"/>
            <session date="9-10-2012" numberOfsessions="2"/>
            <session date="8-10-2012" numberOfsessions="1"/>
            <session date="7-10-2012" numberOfsessions="1.5"/>
        </instructor>
    </course>
    <course id="2" name="poo" nbrecredits="3">
        <instructor id="2" name="Maroun">
            <session date="10-10-2012" numberOfsessions="1"/>
            <session date="9-10-2012" numberOfsessions="3"/>
            <session date="8-10-2012" numberOfsessions="1"/>
            <session date="7-10-2012" numberOfsessions="1.5"/>
        </instructor>
        <instructor id="1" name="youssef">
            <session date="10-10-2012" numberOfsessions="1"/>
            <session date="9-10-2012" numberOfsessions="2"/>
            <session date="8-10-2012" numberOfsessions="1"/>
            <session date="7-10-2012" numberOfsessions="2.5"/>
        </instructor>
        <instructor id="2" name="Samir"></instructor>
    </course>
</tracking>
</tracking>

From this XML I want to calculate the remaining sessions for each course and for each instructor (considering that each course has 15 sessions). So first I wanted to get the number of sessions of each instructor for the first course (AGL). My problem is I can only get the first session which its numberOfsessions is equal to 1.

With this c# code:

var xDoc = XDocument.Load(@"C:\Users\Gaby\Desktop\a.xml");

var homePhone = from phoneno in xDoc.Root.Element("tracking").Elements("course")
                where phoneno.Attribute("name").Value.Equals("AGL")
                select phoneno;

foreach (XElement xEle in homePhone)
{
    Console.WriteLine(xEle.Element("instructor").Element("session").Attribute("numberOfsessions").Value);
}

I tried getting instructors names of each course ( like "AGL" should get me "Youssef" and "Maroun" but it only got me "Youssef"(the first xml element). I even tried to replace the var homePhone with a IEnumerable<string> homePhone but I got a null pointer while parsing it into a list.

And I also have another problem , I want to insert new sessions for every instructor in my xml file using Linq and c#, so is there any way I can edit existing XElements or should I create a new XElement every time?

Thank you for your time.

Upvotes: 0

Views: 60

Answers (2)

jdweng
jdweng

Reputation: 34421

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument xDoc = XDocument.Load(FILENAME);
            List<XElement> homePhone = (from phoneno in xDoc.Descendants("course")
                            where (string)phoneno.Attribute("name") == ("AGL")
                            select phoneno).ToList();

            foreach (XElement instructor in homePhone.Elements("instructor"))
            {
                foreach (XElement session in instructor.Elements("session"))
                {
                    Console.WriteLine("id = '{0}', name = '{1}', credits = '{2}', date = '{3}', number of sessions = '{4}'",
                        (string)instructor.Attribute("id"),
                        (string)instructor.Attribute("name"),
                        (string)instructor.Attribute("nbrecredits"),
                        (string)session.Attribute("date"),
                        (string)session.Attribute("numberOfsessions")
                        );
                }
            }
        }

    }


}

Upvotes: 2

Attila
Attila

Reputation: 66

The problem is in your xml file?

<tracking>
<tracking>
    <course id="1" name="AGL" nbrecredits="3">
        <instructor id="1" name="youssef">
            <session date="10-10-2012" numberOfsessions="1"/>
            <session date="9-10-2012" numberOfsessions="2"/>
            <session date="8-10-2012" numberOfsessions="1"/>
            <session date="7-10-2012" numberOfsessions="1.5"/>
        </instructor>
        <instructor id="2" name="Maroun"/>
            <session date="10-10-2012" numberOfsessions="4"/>
            <session date="9-10-2012" numberOfsessions="1"/>
        </instructor>   <-- Missing XML end tag!
            

        

Upvotes: 0

Related Questions