繁体   English   中英

XML.Linq-根据另一个值获取值

[英]XML.Linq - get value based on another value

我正在尝试使用Linq to XML从基于另一个值的xml中提取一个值。

这是我的xml

<Lead>
<ID>1189226</ID>
<Client>
    <ID>8445254</ID>
    <Name>City of Lincoln Council</Name>
</Client>
<Contact>
    <ID>5747449</ID>
    <Name>Fred Bloggs</Name>
</Contact>
<Owner>
    <ID>36612</ID>
    <Name>Joe Bloggs</Name>
</Owner>
<CustomFields>
    <CustomField>
      <ID>31961</ID>
      <Name>Scotsm_A_n_Authority_Good</Name>
      <Boolean>true</Boolean>
    </CustomField>
    <CustomField>
      <ID>31963</ID>
      <Name>Scotsma_N_Need Not Want</Name>
      <Boolean>false</Boolean>
    </CustomField>
 </CustomFields>

因此,例如,我想尝试在<CustomField>中获取<Boolean>的值,其中<Name>等于应为"true" "Scotsm_A_n_Authority_Good" "true"

我尝试了以下方法:

var xmlAnswers = xe.Element("CustomFields").Element("CustomField").Element("Scotsm_A_n_Authority_Good");

但我得到一个错误,说:

The ' ' character, hexadecimal value 0x20, cannot be included in a name...

有人可以帮忙吗?

您目前正在寻找错误的事物。 您应该在查找CustomField元素,其中的Name元素具有指定的

string target = "Scotsm_A_n_Authority_Good"; // Or whatever
var xmlAnswers = xe.Element("CustomFields")
                   .Elements("CustomField")
                   .Where(cf => (string) cf.Element("Name") == target);

这将为您提供所有匹配的CustomField元素。 然后,您可以通过以下方式获取Boolean值:

foreach (var answer in xmlAnswers)
{
    int id = (int) answer.Element("ID");
    bool value = (bool) answer.Element("Boolean");
    // Use them...
}

(您当然可以在LINQ查询中提取ID和值...)

这是一个xml linq解决方案

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

namespace ConsoleApplication93
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<Lead>" +
                    "<ID>1189226</ID>" +
                    "<Client>" +
                        "<ID>8445254</ID>" +
                        "<Name>City of Lincoln Council</Name>" +
                    "</Client>" +
                    "<Contact>" +
                        "<ID>5747449</ID>" +
                        "<Name>Fred Bloggs</Name>" +
                    "</Contact>" +
                    "<Owner>" +
                        "<ID>36612</ID>" +
                        "<Name>Joe Bloggs</Name>" +
                    "</Owner>" +
                    "<CustomFields>" +
                        "<CustomField>" +
                          "<ID>31961</ID>" +
                          "<Name>Scotsm_A_n_Authority_Good</Name>" +
                          "<Boolean>true</Boolean>" +
                        "</CustomField>" +
                        "<CustomField>" +
                          "<ID>31963</ID>" +
                          "<Name>Scotsma_N_Need Not Want</Name>" +
                          "<Boolean>false</Boolean>" +
                        "</CustomField>" +
                     "</CustomFields>" +
                 "</Lead>";
            XDocument doc = XDocument.Parse(xml);

            //to get all customField
            var results = doc.Descendants("CustomField").Select(x => new
            {
                id = (int)x.Element("ID"),
                name = (string)x.Element("Name"),
                boolean = (Boolean)x.Element("Boolean")
            }).ToList();
            //to get specific
            XElement Scotsm_A_n_Authority_Good = doc.Descendants("CustomField")
                .Where(x => ((string)x.Element("Name") == "Scotsm_A_n_Authority_Good") && (Boolean)(x.Element("Boolean"))).FirstOrDefault();


        }
    }

}

暂无
暂无

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

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