2015-03-30 86 views
0

我有一个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=&quot;mss-abb-aejaljabelalifz-ra&quot;" 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=&quot;mss-abb-aldar-ra&quot;" 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=&quot;mss-abb-aldar-rb&quot;" 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); 
} 
+1

那么究竟什么是你的问题。您提供的Xml无效。它没有RetrievedeviceListResponse的结束标记。你可以加载文件吗?或者,也许你不能读取值? – 2015-03-30 20:02:33

+0

您是否使用'foreach(XElement'上的示例行 – MethodMan 2015-03-30 20:04:10

回答

1

如何像:

var doc = XDocument.Load(@"C:\TestCases\test.xml"); 
     foreach (var node in doc.Descendants().Where(x => "CIName Type Status FriendlyName AccountNo".Contains(x.Name.LocalName))) 
     { 
      Console.WriteLine(string.Format("{0}:{1}", node.Name.LocalName, node.Value)); 
     } 

或纯LINQ:

XDocument.Load(@"C:\TestCases\test.xml").Descendants().Where(x => "CIName Type Status FriendlyName AccountNo".Contains(x.Name.LocalName)).ToList().ForEach(x => Console.WriteLine(string.Format("{0}:{1}", x.Name.LocalName, x.Value))); 

你不关闭你的 “RetrievedeviceListResponse” 在XML太。

+0

我会回答这个问题,因为它包含更少的行数: ) – peter 2015-03-30 20:41:21

+0

:)你也可以用字段字符串数组替换字段字符串,以防你有类似的字段你不想显示。 – Xela 2015-03-30 20:45:22

0
XmlDocument doc = new XmlDocument(); 
doc.Load(path); // Where path is the location of the XML file 

XmlNodeList nodes = doc.DocumentElement.SelectNodes("RetrieveDeviceListResponse"); 

for (int i = 0; i < nodes.Count; i++) 
{ 
    Console.WriteLine("Parent Node - {0}", nodes[i].InnerText); 
    for(int j = 0; j < nodes[i].ChildNodes.Count; j++) 
    { 
     Console.WriteLine("Child Node - {0}", nodes[i].ChildNodes[j].InnerText); 
    } 
} 

这是不是你在找什么?

+0

我只需要'CIName','Type','Status','FriendlyName','AccountNo'节点值 – peter 2015-03-30 20:09:35

+0

如果您在.NET之上,则不建议使用XmlDocument 3.0。XDocument是合适的库 – 2015-03-30 20:12:55

+0

这个答案是行不通的,nodes.Count为零,我使用的是4.5版本 – peter 2015-03-30 20:14:02

1

因此,也许你可以使用这样的事情:

  const string @namespaceName = "http://schemas.hp.com/SM/7"; 
      XDocument xdoc = XDocument.Load(path); 
      var authors = xdoc.Descendants(XName.Get("instance", @namespaceName)); 

      foreach (var author in authors) 
      { 
       string ciName = author.Descendants(XName.Get("CIName", namespaceName)).First().Value; 
       string type = author.Descendants(XName.Get("Type", namespaceName)).First().Value; 
       string frendlyName = author.Descendants(XName.Get("Type", namespaceName)).First().Value; 
       string accountNo = author.Descendants(XName.Get("AccountNo", namespaceName)).First().Value; 
       Console.WriteLine("{0}, {1}, {2}, {3}", ciName, type, frendlyName, accountNo); 
      } 

您必须使用此命名空间,如果你想使用此代码:

using System.Linq; 
using System.Xml.Linq; 
+0

这会起作用,但是xela用较少的代码行来回答 – peter 2015-03-30 20:42:25

+1

没关系。只有你解决问题才重要:) – 2015-03-30 20:46:55

相关问题