c#如何循环遍历 XML 文件

发布时间: 2017/2/26 23:22:23
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个 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:

它可能有助于重新写这篇文章与而不是 linq 的 foreach 循环。(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

赞助商