繁体   English   中英

LINQ to Xml列出错误的查询

[英]LINQ to Xml list wrong query

我想用LINQ从xml文件中获取一些数据,但是我不明白。 这是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>但是对我来说,问题是要处理不同的元素d1d2 ... dn 因为程序在运行时不知道确切的名称。 显然我的尝试是错误的。

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

我想例如将特定客户的所有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.Load方法加载xml,IPs.xml是xml文件的名称

    • .on一旦您在xml中加载了第二行代码,您将以Ips作为字符串列表,其中包含xml中IP元素的内部文本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM