0
我从链接“http://www.codeproject.com/Tips/146237/Reading-XML-documents-using-LINQ”得到了一些启示。用XML代码扩展C#代码
如果我需要检索的Subject1
和Subject2
数据,应该如何在新的C#是通过更换这个代码listBox1.Items.Add(b.Element("Subject").Value.Trim());
C#:
var books = from nodes in System.Xml.Linq.XElement.Load("Books.xml").Elements("Book") select nodes;
if (books != null)
{
foreach (var b in books)
{
listBox1.Items.Add(b.Element("Subject").Value.Trim());
}
}
XML:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book>
<Subject>
<Subject1>Subject1a</Subject1>
<Subject2>Subject1a</Subject2>
</Subject>
<Content>
History,Geography
</Content>
</Book>
<Book>
<Subject>
<Subject1>Subject2b</Subject1>
<Subject2>Subject2b</Subject2>
</Subject>
<Content>
Biology,Chemistry,Physics
</Content>
</Book>
</Books>
对象1&2不是书的直接子女,而是后裔 –
@ChuckSavage你是对的,谢谢:) – user35443