簡體   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