如何解决使用Linq解析XML文档时仅获取第一个值
我的XML文件:
<tracking>
<tracking>
<course id="1" name="AGL" nbrecredits="3">
<instructor id="1" name="youssef">
<session date="10-10-2012" numberOfsessions="1"/>
<session date="9-10-2012" numberOfsessions="2"/>
<session date="8-10-2012" numberOfsessions="1"/>
<session date="7-10-2012" numberOfsessions="1.5"/>
</instructor>
<instructor id="2" name="Maroun"/>
<session date="10-10-2012" numberOfsessions="4"/>
<session date="9-10-2012" numberOfsessions="1"/>
</course>
<course id="2" name="projet de programmationa avancee" nbrecredits="3">
<instructor id="1" name="youssef">
<session date="10-10-2012" numberOfsessions="1"/>
<session date="9-10-2012" numberOfsessions="2"/>
<session date="8-10-2012" numberOfsessions="1"/>
<session date="7-10-2012" numberOfsessions="1.5"/>
</instructor>
</course>
<course id="2" name="poo" nbrecredits="3">
<instructor id="2" name="Maroun">
<session date="10-10-2012" numberOfsessions="1"/>
<session date="9-10-2012" numberOfsessions="3"/>
<session date="8-10-2012" numberOfsessions="1"/>
<session date="7-10-2012" numberOfsessions="1.5"/>
</instructor>
<instructor id="1" name="youssef">
<session date="10-10-2012" numberOfsessions="1"/>
<session date="9-10-2012" numberOfsessions="2"/>
<session date="8-10-2012" numberOfsessions="1"/>
<session date="7-10-2012" numberOfsessions="2.5"/>
</instructor>
<instructor id="2" name="Samir"></instructor>
</course>
</tracking>
</tracking>
根据这个XML,我想计算每门课程和每位讲师的剩余课程(考虑到每门课程有15个课程)。
所以首先,我想获得第一门课程(AGL)的每位讲师的课程数。我的问题是我只能获得其numberOfsessions
等于1
的第一个会话。
使用此C#代码:
var xDoc = XDocument.Load(@"C:\Users\Gaby\Desktop\a.xml");
var homePhone = from phoneno in xDoc.Root.Element("tracking").Elements("course")
where phoneno.Attribute("name").Value.Equals("AGL")
select phoneno;
foreach (XElement xEle in homePhone)
{
Console.WriteLine(xEle.Element("instructor").Element("session").Attribute("numberOfsessions").Value);
}
我尝试获取每门课程的讲师姓名(例如“ AGL”应该让我获得“ Youssef”和“ Maroun”,但它只让我获得“ Youssef”(第一个xml元素)。
我什至尝试将var homePhone
替换为IEnumerable<string> homePhone
,但在将其解析为列表时遇到了空指针。
还有另一个问题,我想使用Linq和c#在xml文件中为每个讲师插入新的会话,那么有什么办法可以编辑现有XElement还是应该每次创建一个新的XElement? >
谢谢您的时间。
解决方法
尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument xDoc = XDocument.Load(FILENAME);
List<XElement> homePhone = (from phoneno in xDoc.Descendants("course")
where (string)phoneno.Attribute("name") == ("AGL")
select phoneno).ToList();
foreach (XElement instructor in homePhone.Elements("instructor"))
{
foreach (XElement session in instructor.Elements("session"))
{
Console.WriteLine("id = '{0}',name = '{1}',credits = '{2}',date = '{3}',number of sessions = '{4}'",(string)instructor.Attribute("id"),(string)instructor.Attribute("name"),(string)instructor.Attribute("nbrecredits"),(string)session.Attribute("date"),(string)session.Attribute("numberOfsessions")
);
}
}
}
}
}
,
问题出在您的xml文件中?
<tracking>
<tracking>
<course id="1" name="AGL" nbrecredits="3">
<instructor id="1" name="youssef">
<session date="10-10-2012" numberOfsessions="1"/>
<session date="9-10-2012" numberOfsessions="2"/>
<session date="8-10-2012" numberOfsessions="1"/>
<session date="7-10-2012" numberOfsessions="1.5"/>
</instructor>
<instructor id="2" name="Maroun"/>
<session date="10-10-2012" numberOfsessions="4"/>
<session date="9-10-2012" numberOfsessions="1"/>
</instructor> <-- Missing XML end tag!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。