從父節點XML標記屬性值。這是我的輸入XML獲取到最後一個子節點
<?xml version="1.0" encoding="UTF-8"?>
<Menu TextField="Menu">
<Menu TextField="Approvals">
<Menu TextField="Vacation/Leave">
<Menu TextField="Vacation Pre-Request Approval" />
<Menu TextField="Vacation Approval" />
</Menu>
<Menu TextField="Late Come Approval" />
<Menu TextField="Loan Approval" />
<Menu TextField="Department Change Approval" />
<Menu TextField="Resignation/Termination">
<Menu TextField="Resignation Approval" />
<Menu TextField="Resignation Clearance" />
</Menu>
</Menu>
<Menu TextField="Employee Transactions">
<Menu TextField="Change Designation" />
<Menu TextField="Organization">
<Menu TextField="Organization Designation & Grade" />
</Menu>
<Menu TextField="Change Grade" />
</Menu>
</Menu>
我想提取TextField
,形成類似下面的字符串:
Approvals > Vacation/Leave > Vacation Pre-Request Approval
Approvals > Vacation/Leave > Vacation Approval
Approvals > Late Come Approval
Approvals > Loan Approval
Approvals > Department Change Approval
Approvals > Resignation/Termination > Resignation Approval
Approvals > Resignation/Termination > Resignation Clearance
Employee Transactions > Change Designation
Employee Transactions > Organization > Organization Designation & Grade
Employee Transactions > Change Grade
我向將TextField的第一個標記值打印到字符串中TextField的最後一個標記值。以下是我嘗試過的代碼,但是打印的所有TextField值不是我需要的結構格式。
var menus = (from menu in XDocument.Parse(xml).Descendants("Menu")
select new
{
TextField = (string)menu.Attribute("TextField")
}).ToList();
無法從'System.Collections.Generic.IEnumerable'轉換爲'System.Xml.Linq.XElement' –
您是對的。我編輯了我的答案。 – Sefe
太棒了!它也打印菜單。我不想在列表中打印。任何想法? –