【问题标题】:how to fetch values from xml file by using xdocument如何使用 xdocument 从 xml 文件中获取值
【发布时间】:2015-03-30 19:48:26
【问题描述】:

我有一个如下所示的 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);
}

【问题讨论】:

  • 那么你的问题到底是什么。您提供的 XML 无效。它没有 RetrievedeviceListResponse 的结束标记。可以加载文件吗?或者您可能无法读取值?
  • 你有没有研究过使用foreach(XElement在线示例

标签: c# .net xml linq


【解决方案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;

【讨论】:

  • 这可行,但 xela 用更少的代码行回答
  • 没关系。解决问题很重要:)
【解决方案2】:

比如:

   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”。

【讨论】:

  • 我会回答这个问题,因为它包含的行数较少:)
  • :) 也可以用字段字符串数组替换字段字符串,以防您有不想显示的类似字段。
【解决方案3】:
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);
    }
}

这是否符合您的要求?

【讨论】:

  • 我只需要 'CIName','Type','Status','FriendlyName','AccountNo' 节点值
  • 如果您在 .NET 3.0 以上,不推荐使用 XmlDocument。 XDocument 是合适的库。
  • 这个答案不起作用,nodes.Count 为零,我使用的是 4.5 版本
  • 是的,很抱歉我对@KSdev 的更多评论,因为您使用的是 4.5,所以您不应该使用 XmlDocument 对象。
  • 我主要是用xdocument,暂时有什么答案会请教
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多