如何遍历XML文件
本文关键字:XML 文件 遍历 何遍历 | 更新日期: 2023-09-27 18:08:27
我有一个XML文件,我被要求从中提取特定的数据并将结果输出到一个TXT文件。这很简单,但现在我知道它必须在一个特定的布局。
我能够拉出course srcid, instructor srcid和response值,但除此之外,我需要做的是将一组evaluation元素中的所有响应写入一行,同时重复course和instructor srcid。
正确输出:
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();
}
}
}
用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