我进行了搜索,但似乎无法基于c#

时间:2018-07-27 17:57:52

标签: c# xml xpath xelement

这是我的示例xml:

<customers group="A">
  <customer ID="1234" phone="555-555-0123" name="Golf Clubs International">
    <services>
      <service type="Golf" premise="89645" priority="0" />
    </services>
    <customFields>
      <customField key="address" value="5431 E Golf Way, Altanta, GA, 31111" />
      <customField key="connection" value="CONNECTED" />
    </customFields>
  </customer>

我需要在服务等于89645的情况下返回customField地址值。我已经尝试过使用XElement的几种不同方式,但是它们似乎都没有针对我的需要进行过滤,即使我将其过滤,我也没有知道如何重新导航以获取同级元素的属性。这是我尝试为前提属性过滤的一些内容。我使用这个网站已有好几年了,这是我第一次陷入困境以致无法发布问题。

        IEnumerable<XElement> tests =
            from el in cust.Elements("services")
            where (string)el.Elements("service").Attributes("premise").ToString() == ID
            select el;

        var y = cust.Element("customers")
                    .Elements("services")
                    .Where(b => b.Element("service").Value == ID)
                    .SingleOrDefault();

        var x = from a in cust.Elements("service")
                where a.Attribute("premise").Value == ID
                select cust.Elements("customfields").Elements("customfield").Attributes("key");

2 个答案:

答案 0 :(得分:1)

在这种情况下,我喜欢使用字典。参见下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication58
{
    class Program
    {
        const string FILENAME = @"C:\TEMP\TEST.XML";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var query = doc.Descendants("customer").Select(x => new{
                premise = (string)x.Descendants("service").FirstOrDefault().Attribute("premise"),
                customFields = x.Descendants("customField").GroupBy(y => (string)y.Attribute("key"), z => (string)z.Attribute("value"))
                .ToDictionary(y => y.Key, z => z.FirstOrDefault())
            }).ToList();

            var results = query.Where(x => x.premise == "89645").FirstOrDefault();


        }
    }
}

这是一个陈述式解决方案

XDocument doc = XDocument.Load(FILENAME);

var query = doc.Descendants("customer").Select(x => new {
    premise = (string)x.Descendants("service").FirstOrDefault().Attribute("premise"),
    customFields = x.Descendants("customField").GroupBy(y => (string)y.Attribute("key"), z => (string)z.Attribute("value"))
    .ToDictionary(y => y.Key, z => z.FirstOrDefault())
}).Where(x => x.premise == "89645")
.FirstOrDefault();

答案 1 :(得分:0)

对于可能会发现此问题的VB用户。

首先提供一些示例数据

    Dim xe As XElement

    xe = <customers group="A">
             <customer ID="1234" phone="555-555-0123" name="Golf Clubs International">
                 <services>
                     <service type="Golf" premise="89645" priority="0"/>
                 </services>
                 <customFields>
                     <customField key="address" value="5431 E Golf Way, Altanta, GA, 31111"/>
                     <customField key="connection" value="CONNECTED"/>
                 </customFields>
             </customer>
             <customer ID="567" phone="555-555-0123" name="Golf Clubs International">
                 <services>
                     <service type="Golf" premise="54698" priority="0"/>
                 </services>
                 <customFields>
                     <customField key="address" value="5431 E Golf Way, Altanta, GA, 31111"/>
                     <customField key="connection" value="CONNECTED"/>
                 </customFields>
             </customer>
             <customer ID="890" phone="555-555-0123" name="Golf Clubs International">
                 <services>
                     <service type="Golf" premise="89645" priority="0"/>
                 </services>
                 <customFields>
                     <customField key="address" value="718 W. High, Jefferson City, MO 65101"/>
                     <customField key="connection" value="CONNECTED"/>
                 </customFields>
             </customer>
         </customers>

然后获取地址的步骤

    Dim rslts As IEnumerable(Of XElement)

    'first get service equals 89645, using above there are two
    rslts = From el In xe...<service>
            Where el.@premise = "89645"
            Select el

    'for each of those find the address
    For Each rel As XElement In rslts
        Dim ie As IEnumerable(Of XElement)
        ie = rel.Parent.Parent.<customFields>.<customField>.Where(Function(cfel)
                                                                      Return cfel.@key = "address"
                                                                  End Function)

        For Each a As XElement In ie
            Debug.WriteLine(a.@value)
        Next
    Next

查询可以缩短,但是这段代码是用来说明正在发生的事情。