我正在學習LINQ,我被困在如何去打印出部門名稱和經理姓名分開。LINQ和xml數據
我有下面的代碼打印出:
Department name: Account
Joe
Department name: Sales
Jack
Department name: Pre-Sales
Sam
Department name: Marketing
Jim
我怎麼能單獨的,所以,它只會在該行打印部門名稱。
string myXML = @"<Departments>
<Department>Account
<Manager>Joe</Manager>
</Department>
<Department>Sales
<Manager>Jack</Manager>
</Department>
<Department>Pre-Sales
<Manager>Sam</Manager>
</Department>
<Department>Marketing
<Manager>Jim</Manager>
</Department>
</Departments>";
XDocument xdoc = new XDocument();
xdoc = XDocument.Parse(myXML);
var DeptResult = xdoc.Element("Departments").Descendants("Department");
var ManagerResult = xdoc.Element("Departments").Descendants("Manager");
foreach (XElement item in DeptResult)
{
Console.WriteLine("Department Name - " + item.Value);
}
對於每個部門都有一個經理? – octavioccl
是的。那是正確的。 – psj01