我如何指定代码以读取仅在 <Keystage3> 节点中的所有 <Module>
How can i specify the code to read through all <Module> that are in only the <Keystage3> node
这是我当前的代码,它输出所有带有名称 Module 的标签,但是我想指定它只读取 <keystage3>
,因此读取该标签内的所有模块。
XElement root = XElement.Load("Data.xml");
var subjects = from subject in root.Descendants()
where subject.Name.LocalName.Contains("Subject")
select new
{
subname = subject.Element("subjectName").Value,
subid = subject.Element("subjectId").Value,
subvalue = subject.Element("subjectvalue").Value
};
foreach (var subject in subjects)
{
Console.WriteLine(subject);
//you can use subject like this:
string subName = subject.subname;
string subID = subject.subid;
string subValue = subject.subvalue;
TextBox[] info = new TextBox[6];
textBox1.AppendText("Subject Name : " + subName + " Subject Id : " + subID + " Subject Value : " + subValue + " " );
}
我的 xml 文件包含:
<PersonDetails>
<PersonTitle>Teacher</PersonTitle>
<Keystage3>
<Subject>
<subjectName>maths</subjectName>
<subjectId>qq1</subjectId>
<subjectvalue>20</subjectvalue>
<subjectscore />
</Subject>
<Subject>
<subjectName>science</subjectName>
<subjectId>sla1s</subjectId>
<subjectvalue>25</subjectvalue>
<subjectscore />
</Subject>
</Keystage3>
<Keystage4>
<Subject>
<subjectName>ICT</subjectName>
<subjectId>a1</subjectId>
<subjectvalue>10</subjectvalue>
<subjectscore />
</Subject>
</Keystage4>
</PersonDetails>
所以我的问题是如何指定代码来读取仅在 <Keystage3>
中的所有 <Module>
,因为当前代码为我提供了整个 <Module>
xml 文件。
您通过 "keystage3" 预过滤后代,然后像您已经做的那样过滤:
internal class Program
{
private static string txt = @"
<PersonDetails><PersonTitle>Teacher</PersonTitle><Keystage3><Subject>
<subjectName>maths</subjectName><subjectId>qq1</subjectId><subjectvalue>20</subjectvalue>
<subjectscore /></Subject><Subject><subjectName>science</subjectName>
<subjectId>sla1s</subjectId><subjectvalue>25</subjectvalue><subjectscore />
</Subject></Keystage3><Keystage4><Subject><subjectName>ICT</subjectName>
<subjectId>a1</subjectId><subjectvalue>10</subjectvalue><subjectscore />
</Subject></Keystage4></PersonDetails>";
static void Main()
{
XElement root = XElement.Parse(txt); // Load -> Parse
var subjects = from stage in root.Descendants()
where stage.Name.LocalName == "Keystage3"
from subject in stage.Descendants()
where subject.Name.LocalName.Contains("Subject")
select new
{
subname = subject.Element("subjectName").Value,
subid = subject.Element("subjectId").Value,
subvalue = subject.Element("subjectvalue").Value
};
foreach (var subject in subjects)
Console.WriteLine(subject);
Console.ReadLine();
}
}
输出:
{ subname = maths, subid = qq1, subvalue = 20 }
{ subname = science, subid = sla1s, subvalue = 25 }
这是我当前的代码,它输出所有带有名称 Module 的标签,但是我想指定它只读取 <keystage3>
,因此读取该标签内的所有模块。
XElement root = XElement.Load("Data.xml");
var subjects = from subject in root.Descendants()
where subject.Name.LocalName.Contains("Subject")
select new
{
subname = subject.Element("subjectName").Value,
subid = subject.Element("subjectId").Value,
subvalue = subject.Element("subjectvalue").Value
};
foreach (var subject in subjects)
{
Console.WriteLine(subject);
//you can use subject like this:
string subName = subject.subname;
string subID = subject.subid;
string subValue = subject.subvalue;
TextBox[] info = new TextBox[6];
textBox1.AppendText("Subject Name : " + subName + " Subject Id : " + subID + " Subject Value : " + subValue + " " );
}
我的 xml 文件包含:
<PersonDetails>
<PersonTitle>Teacher</PersonTitle>
<Keystage3>
<Subject>
<subjectName>maths</subjectName>
<subjectId>qq1</subjectId>
<subjectvalue>20</subjectvalue>
<subjectscore />
</Subject>
<Subject>
<subjectName>science</subjectName>
<subjectId>sla1s</subjectId>
<subjectvalue>25</subjectvalue>
<subjectscore />
</Subject>
</Keystage3>
<Keystage4>
<Subject>
<subjectName>ICT</subjectName>
<subjectId>a1</subjectId>
<subjectvalue>10</subjectvalue>
<subjectscore />
</Subject>
</Keystage4>
</PersonDetails>
所以我的问题是如何指定代码来读取仅在 <Keystage3>
中的所有 <Module>
,因为当前代码为我提供了整个 <Module>
xml 文件。
您通过 "keystage3" 预过滤后代,然后像您已经做的那样过滤:
internal class Program
{
private static string txt = @"
<PersonDetails><PersonTitle>Teacher</PersonTitle><Keystage3><Subject>
<subjectName>maths</subjectName><subjectId>qq1</subjectId><subjectvalue>20</subjectvalue>
<subjectscore /></Subject><Subject><subjectName>science</subjectName>
<subjectId>sla1s</subjectId><subjectvalue>25</subjectvalue><subjectscore />
</Subject></Keystage3><Keystage4><Subject><subjectName>ICT</subjectName>
<subjectId>a1</subjectId><subjectvalue>10</subjectvalue><subjectscore />
</Subject></Keystage4></PersonDetails>";
static void Main()
{
XElement root = XElement.Parse(txt); // Load -> Parse
var subjects = from stage in root.Descendants()
where stage.Name.LocalName == "Keystage3"
from subject in stage.Descendants()
where subject.Name.LocalName.Contains("Subject")
select new
{
subname = subject.Element("subjectName").Value,
subid = subject.Element("subjectId").Value,
subvalue = subject.Element("subjectvalue").Value
};
foreach (var subject in subjects)
Console.WriteLine(subject);
Console.ReadLine();
}
}
输出:
{ subname = maths, subid = qq1, subvalue = 20 }
{ subname = science, subid = sla1s, subvalue = 25 }