簡體   English   中英

如何通過使用xdocument從xml文件中獲取值

[英]how to fetch values from xml file by using xdocument

我有一個xmfile文件,如下所示,我必須在其中獲取所有節點值CIName,Type,Status,FriendlyName,AccountNo 我試圖通過使用XDocument來獲得結果,但沒有成功。

<?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);
}

怎么樣:

   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)));

您也不會在xml中關閉“ RetrievedeviceListResponse”。

因此,也許您可​​以使用如下所示的內容:

            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;
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);
    }
}

這是您想要的嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM