如何遍历XML文件

时间:2015-08-17 05:47:35

标签: c# xml linq visual-studio

我有一个XML文件,我被要求从中提取特定数据并将结果输出到TXT文件。这很简单,但现在我已经知道它必须采用特定的布局。

我能够提取课程srcid 讲师srcid 响应值,但除此之外我还需要做什么将一组评估元素中的所有响应写入一行,同时重复课程和讲师srcids。

正确输出:

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

XML文件:

<?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>

当前C#代码:

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(); 
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用foreach循环而不是linq重写它可能会有所帮助。 (Linq很棒,但在一行中编写一个非常复杂的案例变得棘手)。

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(); 
        }

在此处试试:https://dotnetfiddle.net/ft3yMw