我有一个xmfile文件,如下所示,我必须获取所有节点值CIName,Type,Status,FriendlyName,AccountNo
。我试图通过使用XDocument
而没有成功。如何通过使用xdocument从xml文件中获取值
<?xml version="1.0" encoding="utf-8"?>
<RetrievedeviceListResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" schemaRevisionLevel="0" returnCode="0" status="SUCCESS" message="Success" schemaRevisionDate="2015-03-24">
<instance uniquequery="file.device,logical.name="mss-abb-aejaljabelalifz-ra"" query="" xmlns="http://schemas.hp.com/SM/7">
<file.device type="Structure">
<CIName type="String">mss-abb-aejaljabelalifz-ra</CIName>
<Type type="String">networkcomponents</Type>
<Status type="String">In use</Status>
<FriendlyName type="String">Jabel Ali Free Zone</FriendlyName>
<Company type="String">ABB - MWAN</Company>
</file.device>
<file.networkcomponents type="Structure">
<AccountNo type="String">1444016683</AccountNo>
</file.networkcomponents>
<attachments xsi:nil="true" />
</instance>
<instance uniquequery="file.device,logical.name="mss-abb-aldar-ra"" query="" xmlns="http://schemas.hp.com/SM/7">
<file.device type="Structure">
<CIName type="String">mss-abb-aldar-ra</CIName>
<Type type="String">networkcomponents</Type>
<Status type="String">In use</Status>
<FriendlyName type="String">Al Dar AUH Main</FriendlyName>
<Company type="String">ABB - MWAN</Company>
</file.device>
<file.networkcomponents type="Structure">
<AccountNo type="String">1222229614</AccountNo>
</file.networkcomponents>
<attachments xsi:nil="true" />
</instance>
<instance uniquequery="file.device,logical.name="mss-abb-aldar-rb"" query="" xmlns="http://schemas.hp.com/SM/7">
<file.device type="Structure">
<CIName type="String">mss-abb-aldar-rb</CIName>
<Type type="String">networkcomponents</Type>
<Status type="String">In use</Status>
<FriendlyName type="String">Al Dar-AUH-Backup</FriendlyName>
<Company type="String">ABB - MWAN</Company>
</file.device>
<file.networkcomponents type="Structure">
<AccountNo type="String">1222222368</AccountNo>
</file.networkcomponents>
<attachments xsi:nil="true" />
</instance>
</RetrievedeviceListResponse>
我用下面的代码
XDocument xdoc = XDocument.Load(path);
var authors = xdoc.Descendants("RetrievedeviceListResponse");
foreach (var author in authors)
{
Console.WriteLine("{0}{1}",author.Name, author.Value);
}
那么究竟什么是你的问题。您提供的Xml无效。它没有RetrievedeviceListResponse的结束标记。你可以加载文件吗?或者,也许你不能读取值? – 2015-03-30 20:02:33
您是否使用'foreach(XElement'上的示例行 – MethodMan 2015-03-30 20:04:10