给定一个包含以下内容的 xml:
<detailedreport analysis_size_bytes="220358752" engine_version="127144">
<modules>
<module name="Base.dll" compiler="MSIL_MSVC14_X86" os="Win32" architecture="CIL32" loc="764981" score="56" numflawssev0="6" numflawssev1="0" numflawssev2="6" numflawssev3="264" numflawssev4="4" numflawssev5="2"/>
<module name="EpisodeDefinition.dll" compiler="MSIL_MSVC14_X86" os="Win32" architecture="CIL32" loc="1165845" score="66" numflawssev0="1" numflawssev1="0" numflawssev2="56" numflawssev3="149" numflawssev4="1" numflawssev5="0"/>
<module name="UI.dll" compiler="MSIL_MSVC14_X86" os="Win32" architecture="CIL32" loc="1165845" score="79" numflawssev0="0" numflawssev1="0" numflawssev2="10" numflawssev3="73" numflawssev4="0" numflawssev5="0"/>
</modules>
</detailedreport>
使用 Json.Net 来处理它:
XDocument xmlDoc = XDocument.Load(@"myxmlfile.xml");
var json = JsonConvert.SerializeXNode(xmlDoc);
var jobj = JObject.Parse(json);
var modules = jobj.SelectTokens("$..modules.module");
当我期望 3 个时,我只得到一个"模块",而且我不知道如何从属性中获取信息?具体来说,我希望能够获得模块名称的列表。
XDocument xmlDoc = XDocument.Load(@"myxmlfile.xml");
var rows = from row in xmlDoc.XPathSelectElements("//node()[local-name(.) = 'module']") select row;
var modules = rows.SelectMany(row => row.Attributes().Where(a => a.Name == "name").Select(a => a.Value));
modules.Dump();