查找字符串中包含属性值的XmlNode [英] Find XmlNode where attribute value is contained in string

查看:154
本文介绍了查找字符串中包含属性值的XmlNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件...

I have an xml file...

<?xml version="1.0" encoding="UTF-8"?>
<items defaultNode="1">
    <default contentPlaceholderName="pageContent" template="" genericContentItemName="" />
    <item urlSearchPattern="connections-learning" contentPlaceholderName="pageContent" template="Connections Learning Content Page" genericContentItemName="" />
    <item urlSearchPattern="online-high-school" contentPlaceholderName="pageContent" template="" genericContentItemName="" />
</items>

我正在尝试查找字符串urlSearchPattern中包含urlSearchPattern属性的第一个节点.我遇到麻烦的地方是找到属性包含在字符串值中而不是字符串值包含在属性中的节点.

I am trying to find the first node where the urlSearchPattern attribute is contained in the string urlSearchPattern. Where I'm having trouble is finding the nodes where the attribute is contained in the string value instead of the string value be contained in the attribute.

到目前为止,这是我的尝试.这将找到属性值中包含字符串值的firstOrDefault节点(我需要相反的意思)...

Here's my attempt so far. This will find the firstOrDefault node where the string value is contained in the attribute (I need the opposite)...

string urlSearchPattern = Request.QueryString["aspxerrorpath"];
MissingPageSettingsXmlDocument missingPageSettingsXmlDocument = new MissingPageSettingsXmlDocument();
XmlNode missingPageItem = missingPageSettingsXmlDocument.SelectNodes(ITEM_XML_PATH).Cast<XmlNode>().Where(item => item.Attributes["urlSearchPattern"].ToString().ToLower().Contains(urlSearchPattern)).FirstOrDefault();

推荐答案

好吧...然后反转!

var result = missingPageSettingsXmlDocument
                .SelectNodes(ITEM_XML_PATH)
                .Cast<XmlNode>()
                .FirstOrDefault(
                    m => m.Attributes != null && 
                    m.Attributes["urlSearchPattern"] != null && 
                    urlSearchPattern.Contains(m.Attributes["urlSearchPattern"].ToString().ToLower())
                 );

这篇关于查找字符串中包含属性值的XmlNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆