LINQ到Xml列表错误查询

本文关键字:错误 查询 列表 Xml LINQ | 更新日期: 2023-09-27 18:12:03

我想从xml文件中获得LINQ的一些数据,但我没有得到它。这是xml文件:

<Data>
  <Customer>
    <Name>bla1</Name>
      <d1>
        <IP>888.888.888.888</IP>
        <UserLogin>userxy</UserLogin>
        <UserPw>pwxy</UserPw>
      </d1>
      <d2>
        <IP>889.889.889.889</IP>
        <UserLogin>userzp</UserLogin>
        <UserPw>pwzp</UserPw>
      </d2>
  </Customer>
</Data>

我想获得例如特定客户的所有ip到List<string>,但我的问题是处理不同的元素d1, d2dn。因为程序在运行时不知道确切的名称。显然我的尝试是错误的…

XDocument root = XDocument.Load(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "''Xml.xml");
List<string> IP = new List<string>();
IP = root.Descendants("Customer").Descendants("Name")
    .Where(x => x.Element("Name").Value == name)
    .Select(x => x.Element("Name").Descendants("IP").ToList<string>();

LINQ到Xml列表错误查询

我想获得例如一个特定客户的所有ip到一个列表

我猜你正在寻找这样的东西(使用Linq2Xml和Xpath)

var xDoc = XDocument.Parse(xmlstring); // XDocument.Load(filename)
string custName = "bla1";
var ips = xDoc.XPathSelectElement("//Customer[Name[text()='" + custName  + "']]")
            .Descendants("IP")
            .Select(x => (string)x)
            .ToList();

编辑

让我们用纯Linq

@Bobson开心
var ips = xDoc.Descendants("Customer")
        .FirstOrDefault(c=>c.Elements("Name").Any(e=>(string)e==custName))
        .Descendants("IP")
        .Select(x => (string)x)
        .ToList();

下面是一个基于LINQ查询语法的解决方案:

string customerName = "bla1";
XElement dataElem = XElement.Parse(dataXml);
var ipAddresses =
    from customerElem in dataElem.Elements("Customer")
    where (string)customerElem.Element("Name") == customerName
    from ipElem in customerElem.Descendants("IP")
    select (string)ipElem;
foreach (var ipAddress in ipAddresses)
{
    Console.WriteLine("[{0}]", ipAddress);
}

请参阅下面的完整工作示例,前面是预期的输出。(请参阅现场演示)

预期输出:

[888.888.888.888]
[889.889.889.889]

计划:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
namespace LinqQuerySyntaxDemo
{
    public class Program
    {
        static public void Main(string[] args)
        {
            string customerName = "bla1";
            XElement dataElem = XElement.Parse(GetXml());
            var ipAddresses =
                from customerElem in dataElem.Elements("Customer")
                where (string)customerElem.Element("Name") == customerName
                from ipElem in customerElem.Descendants("IP")
                select (string)ipElem;
            foreach (var ipAddress in ipAddresses)
            {
                Console.WriteLine("[{0}]", ipAddress);
            }
        }
        static string GetXml()
        {
            return
               @"<Data>
                   <Customer>
                     <Name>bla1</Name>
                       <d1>
                         <IP>888.888.888.888</IP>
                         <UserLogin>userxy</UserLogin>
                         <UserPw>pwxy</UserPw>
                       </d1>
                       <d2>
                         <IP>889.889.889.889</IP>
                         <UserLogin>userzp</UserLogin>
                         <UserPw>pwzp</UserPw>
                       </d2>
                   </Customer>
                 </Data>";
        }
    }
}

试试这个,它为我工作

var xml = XDocument.Load("IPs.xml");
            var Ips = from ip in xml.Descendants("IP")
                      select ip.Value;

从上面的代码

  • 第1行将帮助您使用XDocument加载xml。加载方法,xml是xml文件

    的名称。
    • 。一旦你用第二行代码加载了xml,你将有ip作为字符串列表,其中包含的内部文本xml
    • 中的元素IP