<?xml version="1.0" encoding="utf-8" ?>
<Licenses>
<License>
<LicenseType>Temporary License</LicenseType>
<BundleType>Line</BundleType>
<Features>
<Feature>
<Value>Full</Value>
<Status>Full Access</Status>
<AccessLevel>Full</AccessLevel>
</Feature>
<Feature>
<Name>EnhancedUserAccounts</Name>
<LocalisedName>Enhanced User Accounts</LocalisedName>
<Value>Full</Value>
<Status>Full Access</Status>
<AccessLevel>Full</AccessLevel>
</Feature>
</Features>
</License>
<License>
<LicenseType>Temporary License</LicenseType>
<BundleType>Line</BundleType>
<Features>
<Feature>
<Value>Full</Value>
<Status>Full Access</Status>
<AccessLevel>Full</AccessLevel>
</Feature>
<Feature>
<Name>EnhancedUserAccounts</Name>
<LocalisedName>Enhanced User Accounts</LocalisedName>
<Value>Full</Value>
<Status>Full Access</Status>
<AccessLevel>Full</AccessLevel>
</Feature>
</Features>
</License>
</Licenses>
如果我的BundleType = line,如何将功能列表读入IEnumerable
答案 0 :(得分:0)
您可以使用XDocument轻松完成此操作
var xDocument = XDocument.Parse(XML)
或
var xDocument = XDocuemnt.Load(FilePath)
然后:
xDocument.Descendants("LicenseType")
这将获取所有来自LicenseType的节点
要测试该BundleType元素,您可以执行以下操作:
nodes.Where(n=>n.Element("BundleType") == "Line").Decesendants("Features")
它将获得父项的<BundleType>
为“线”的所有功能
编辑:根据评论,获得所有功能:
xDocument.Desecendants("Features")
这将使<Feature>
元素下的所有<Features>
节点
答案 1 :(得分:0)
根据我在您的帖子中更新的xml使用以下内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication70
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("License").Where(x => (string)x.Element("BundleType") == "Line")
.Select(x => x.Descendants("Feature").ToList()).ToList();
}
}
}