Abfragen von XML-Dokument mithilfe von Linq
Ich versuche, Linq ein xml-Dokument, ich bin nicht in der Lage, um die Abfrage die inneren Elemente, wie Sie sehen können aus dem code unten, was ich versuche zu tun. Ich möchte alle Datensätze die einen bestimmten Namen... Bitte um Hilfe.
<?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>
Und jetzt die Quelle
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();
}
}
InformationsquelleAutor Ramesh Sivaraman | 2012-11-11
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ersetzen Sie Ihre
LINQ where
Abfrage mit diesem ein -Descendants
stattAttributes
.Vielen Dank für die..
InformationsquelleAutor Rohit Vats