1
我想Linq一個xml文檔,我無法查詢內部元素,因爲您可以從下面的代碼中看到我正在嘗試執行的操作。我想獲得所有具有特定名稱的記錄...請幫助。使用Linq查詢XML文檔
<?xml version="1.0" encoding="utf-8" ?>
<Student>
<Person name="John" city="Auckland" country="NZ" />
<Person>
<Course>GDICT-CN</Course>
<Level>7</Level>
<Credit>120</Credit>
<Date>129971035565221298</Date>
</Person>
<Person>
<Course>GDICT-CN</Course>
<Level>7</Level>
<Credit>120</Credit>
<Date>129971036040828501</Date>
</Person>
</Student>
而現在源
class Program
{
static void Main(string[] args)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
XDocument xDoc = XDocument.Load(path + "\\Student Data\\data.xml");
IEnumerable<XElement> rows =
from row in xDoc.Descendants("Person")
where (string)row.Attribute("Course") == "GDICT-CN"
select row;
foreach(XElement xEle in rows)
{
IEnumerable<XAttribute>attlist =
from att in xEle.DescendantsAndSelf().Attributes()
select att;
foreach(XAttribute xatt in attlist)
{
Console.WriteLine(xatt);
}
foreach (XElement elemnt in xEle.Descendants())
{
Console.WriteLine(elemnt.Value);
}
Console.WriteLine("-------------------------------------------");
}
Console.ReadLine();
}
}
如前所述,你應該查看一下'Descendants'而不是'Attributes'。 –
謝謝你.. –