找到你要的答案

Q:How to get xml log elements by searching the specific string/text using LINQ

Q:如何通过搜索特定的字符串或文本使用LINQ到XML日志元素

I have a XML like this below:

<status_log>
  <log>
    <status>CREATED</status>
    <detail></detail>
    <datetime>03/17/2015 10:06:02</datetime>
  </log>
  <log>
    <status>AC_VALIDATION</status>
    <detail>Credit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/17/2015 10:06:04</datetime>
  </log>
  <log>
    <status>CREATED</status>
    <detail>Debit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/17/2015 10:06:05</datetime>
  </log>
  <log>
    <status>INVALID</status>
    <detail>Request ID: 110895, Transaction Number: A92C18E9 ,Status: 9, Error Code: 0</detail>
    <datetime>03/18/2015 20:14:34</datetime>
  </log>
  <log>
    <status>UPDATED</status>
    <detail></detail>
    <datetime>03/19/2015 06:06:03</datetime>
  </log>
  <log>
    <status>AC_VALIDATION</status>
    <detail>Credit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/19/2015 06:06:05</datetime>
  </log>
  <log>
    <status>CREATED</status>
    <detail>Debit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/19/2015 06:06:05</datetime>
  </log>
  <log>
    <status>UPDATED</status>
    <detail></detail>
    <datetime>03/19/2015 06:06:12</datetime>
  </log>
</status_log>

I want to get the <log> xml only for elements that have Credit Transaction Response as value in the <detail> child element.

我有一个这样的XML如下:

<status_log>
  <log>
    <status>CREATED</status>
    <detail></detail>
    <datetime>03/17/2015 10:06:02</datetime>
  </log>
  <log>
    <status>AC_VALIDATION</status>
    <detail>Credit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/17/2015 10:06:04</datetime>
  </log>
  <log>
    <status>CREATED</status>
    <detail>Debit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/17/2015 10:06:05</datetime>
  </log>
  <log>
    <status>INVALID</status>
    <detail>Request ID: 110895, Transaction Number: A92C18E9 ,Status: 9, Error Code: 0</detail>
    <datetime>03/18/2015 20:14:34</datetime>
  </log>
  <log>
    <status>UPDATED</status>
    <detail></detail>
    <datetime>03/19/2015 06:06:03</datetime>
  </log>
  <log>
    <status>AC_VALIDATION</status>
    <detail>Credit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/19/2015 06:06:05</datetime>
  </log>
  <log>
    <status>CREATED</status>
    <detail>Debit Transaction Response: 0 - , Error:  -</detail>
    <datetime>03/19/2015 06:06:05</datetime>
  </log>
  <log>
    <status>UPDATED</status>
    <detail></detail>
    <datetime>03/19/2015 06:06:12</datetime>
  </log>
</status_log>

我想找到<;日志>;XML只对有信用交易响应在& lt值元素的子元素;细节& gt。

answer1: 回答1:

You simple need a Where extension method to filter:-

var result = xdoc.Descendants("log")
                .Where(x => (string)x.Element("detail") == "Credit Transaction Response");

Update:

If you want to filter just by Credit Transaction Response and not by complete text you can use StartsWith like this:-

var result = xdoc.Descendants("log")
                 .Where(x => (string)x.Element("detail") != null  
       ? x.Element("detail").Value.ToString().StartsWith("Credit Transaction Response") 
       : false);

你需要一个简单的扩展方法来过滤:—

var result = xdoc.Descendants("log")
                .Where(x => (string)x.Element("detail") == "Credit Transaction Response");

更新:

如果你想过滤只是信用交易的反应和不完整的文本,你可以使用从这样:—

var result = xdoc.Descendants("log")
                 .Where(x => (string)x.Element("detail") != null  
       ? x.Element("detail").Value.ToString().StartsWith("Credit Transaction Response") 
       : false);
answer2: 回答2:

Simple Solution. Use StartsWith().

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
                "<status_log>" +
                  "<log>" +
                    "<status>CREATED</status>" +
                    "<detail></detail>" +
                    "<datetime>03/17/2015 10:06:02</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>AC_VALIDATION</status>" +
                    "<detail>Credit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/17/2015 10:06:04</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>CREATED</status>" +
                    "<detail>Debit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/17/2015 10:06:05</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>INVALID</status>" +
                    "<detail>Request ID: 110895, Transaction Number: A92C18E9 ,Status: 9, Error Code: 0</detail>" +
                    "<datetime>03/18/2015 20:14:34</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>UPDATED</status>" +
                    "<detail></detail>" +
                    "<datetime>03/19/2015 06:06:03</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>AC_VALIDATION</status>" +
                    "<detail>Credit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/19/2015 06:06:05</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>CREATED</status>" +
                    "<detail>Debit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/19/2015 06:06:05</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>UPDATED</status>" +
                    "<detail></detail>" +
                    "<datetime>03/19/2015 06:06:12</datetime>" +
                  "</log>" +
                "</status_log>";

            XDocument doc = XDocument.Parse(input);

            var filteredLogs = doc.Descendants("log").Where(x => x.Element("detail").Value.StartsWith("Credit Transaction Response:")).ToList();
        }
    }
}
​

简单的解决方案。使用startswith()。

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = 
                "<status_log>" +
                  "<log>" +
                    "<status>CREATED</status>" +
                    "<detail></detail>" +
                    "<datetime>03/17/2015 10:06:02</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>AC_VALIDATION</status>" +
                    "<detail>Credit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/17/2015 10:06:04</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>CREATED</status>" +
                    "<detail>Debit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/17/2015 10:06:05</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>INVALID</status>" +
                    "<detail>Request ID: 110895, Transaction Number: A92C18E9 ,Status: 9, Error Code: 0</detail>" +
                    "<datetime>03/18/2015 20:14:34</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>UPDATED</status>" +
                    "<detail></detail>" +
                    "<datetime>03/19/2015 06:06:03</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>AC_VALIDATION</status>" +
                    "<detail>Credit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/19/2015 06:06:05</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>CREATED</status>" +
                    "<detail>Debit Transaction Response: 0 - , Error:  -</detail>" +
                    "<datetime>03/19/2015 06:06:05</datetime>" +
                  "</log>" +
                  "<log>" +
                    "<status>UPDATED</status>" +
                    "<detail></detail>" +
                    "<datetime>03/19/2015 06:06:12</datetime>" +
                  "</log>" +
                "</status_log>";

            XDocument doc = XDocument.Parse(input);

            var filteredLogs = doc.Descendants("log").Where(x => x.Element("detail").Value.StartsWith("Credit Transaction Response:")).ToList();
        }
    }
}
​
xml  linq