How to loop through an XML file

I have an XML file that I was asked to pull specific data from and output the results to a TXT file. This was simple enough but now I have learned that it has to be in a specific layout.

I am able to pull the course srcid, instructor srcid, and the response values but what I need to do in addition to that is have all responses within a set of evaluation elements be written on one line while repeating the course and instructor srcids.

Correct Output:

0123450123456
12345612345674444443333-1-13-1-134
345678345678944444444444444444444444
345678345678933444444444344443343444

XML File:

<?xml version="1.0" encoding="UTF-8"?>
<data>
     <term>
        <courses>
            <course srcid="012345">
                <instructor srcid="0123456"></instructor>
            </course>
            <course srcid="123456">
                <instructor srcid="1234567"></instructor>
                <evaluations>
                    <evaluation decline="false" sequence_num="0001">
                        <response questionId="32">Yes</response>
                        <response questionId="34">4</response>
                        <response questionId="35">4</response>
                        <response questionId="36">4</response>
                        <response questionId="37">4</response>
                        <response questionId="38">4</response>
                        <response questionId="39">4</response>
                        <response questionId="40">3</response>
                        <response questionId="41">3</response>
                        <response questionId="42">3</response>
                        <response questionId="43">3</response>
                        <response questionId="44">-1</response>
                        <response questionId="45">-1</response>
                        <response questionId="46">3</response>
                        <response questionId="47">-1</response>
                        <response questionId="48">-1</response>
                        <response questionId="49">3</response>
                        <response questionId="50">4</response>
                    </evaluation>
                </evaluations>
            </course>
            <course srcid="345678">
                <instructor srcid="3456789"></instructor>
                <evaluations>
                    <evaluation decline="false" sequence_num="0002">
                        <response questionId="32">NO</response>
                        <response questionId="33">YES</response>
                        <response questionId="34">4</response>
                        <response questionId="35">4</response>
                        <response questionId="36">4</response>
                        <response questionId="37">4</response>
                        <response questionId="38">4</response>
                        <response questionId="39">4</response>
                        <response questionId="40">4</response>
                        <response questionId="41">4</response>
                        <response questionId="42">4</response>
                        <response questionId="43">4</response>
                        <response questionId="44">4</response>
                        <response questionId="45">4</response>
                        <response questionId="46">4</response>
                        <response questionId="47">4</response>
                        <response questionId="48">4</response>
                        <response questionId="49">4</response>
                        <response questionId="50">4</response>
                        <response questionId="51">4</response>
                        <response questionId="52">4</response>
                        <response questionId="53">4</response>
                        <response questionId="54">4</response>
                        <response questionId="55">4</response>
                        <response questionId="56">4</response>
                    </evaluation>
                    <evaluation decline="false" sequence_num="0003">
                        <response questionId="32">YES</response>
                        <response questionId="33">YES</response>
                        <response questionId="34">3</response>
                        <response questionId="35">3</response>
                        <response questionId="36">4</response>
                        <response questionId="37">4</response>
                        <response questionId="38">4</response>
                        <response questionId="39">4</response>
                        <response questionId="40">4</response>
                        <response questionId="41">4</response>
                        <response questionId="42">4</response>
                        <response questionId="43">4</response>
                        <response questionId="44">4</response>
                        <response questionId="45">3</response>
                        <response questionId="46">4</response>
                        <response questionId="47">4</response>
                        <response questionId="48">4</response>
                        <response questionId="49">4</response>
                        <response questionId="50">3</response>
                        <response questionId="51">3</response>
                        <response questionId="52">4</response>
                        <response questionId="53">3</response>
                        <response questionId="54">4</response>
                        <response questionId="55">4</response>
                        <response questionId="56">4</response>
                    </evaluation>
                </evaluations>
            </course>
        </courses>
    </term>
</data>

Current C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> questions = new List<string>(){ "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56" };
            XDocument xmldata = XDocument.Load("C:\\Users\\Test\\test.xml");
            var courses = from xml in xmldata.Root.Descendants("course")
                          select new { 
                              course = (string) xml.Attribute("srcid").Value,
                              instructor = (string) xml.Element("instructor").Attribute("srcid").Value,
                              eval = from answer in xml.Descendants("response")
                                     where questions.Contains(answer.Attribute("questionId").Value)
                                     select answer
            };
            foreach (var xml in courses)
            {
                string output = xml.course + xml.instructor;
                foreach (string answer in xml.eval)
                    output += answer;
                Console.Write(output + "\r\n");
            }
            Console.ReadLine(); 
        }
    }
}

Answers


It might help to re-write this with foreach loops instead of linq. (Linq is awesome, but writing a very complex case in one line gets tricky).

static void Main(string[] args)
        {
            List<string> questions = new List<string>(){ "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56" };
            XDocument xmldata = XDocument.Load("C:\\Users\\Test\\test.xml");
            foreach (var courseNode in xmldata.Root.Descendants("course"))
            {
                string course = (string) courseNode.Attribute("srcid").Value;
                string instructor = (string) courseNode.Element("instructor").Attribute("srcid").Value;
                int evaluationCount = 0;
                foreach (var evaluationNode in courseNode.Descendants("evaluation"))
                {
                    var eval = from answer in evaluationNode.Descendants("response")
                                     where questions.Contains(answer.Attribute("questionId").Value)
                                     select answer;
                    string output = course + instructor;
                    foreach (string answer in eval)
                        output += answer;
                    Console.Write(output + "\r\n");
                    ++evaluationCount;
                }
                if (evaluationCount == 0)
                {
                    Console.WriteLine(course + instructor);
                }
            }
            Console.ReadLine(); 
        }

Try it here: https://dotnetfiddle.net/ft3yMw


Need Your Help

Assigning each field of an array to a variable, foreach loop

c# asp.net arrays

I have a string array, and a for each loop. I want to loop through the array and do something to each field and then output the result of each field to a new variable.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.