2

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>

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.

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();
1
  • can you give example of the outputs you're expecting, I don't follow what you mean by 'attribute is contained in the string value' and 'string value be contained in the attribute' Commented Jun 29, 2012 at 14:38

2 Answers 2

1

well... then invert !

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())
                 );
Sign up to request clarification or add additional context in comments.

Comments

1

Using this Xml Library, and providing your ITEM_XML_PATH looks something like: //item

XElement root = XElement.Load(file); // or .Parse(string)
var matches = root.XPath("//item[contains({0}, {1}, false)]", 
    urlSearchPattern, new NodeSet("@urlSearchPattern"));

false for converting all values with .ToLower() and reversed pattern with the nodeset, it will do the search of pattern.Contains(nodeset).

If you have items without a urlSearchPattern or their value is "", you can add and . != '' to the xpath expression to remove them from the result.

The library is in its infancy, so if your ITEM_XML_PATH is real complicated this might not work for you.

Update: Based on Pawel's comments, using the included Linq-to-Xml XPath version:

root.XPathSelectElements(
    "//item[contains('" + urlSearchPattern + "', @urlSearchPattern)]");

5 Comments

Linq to Xml has built-in support for XPath: msdn.microsoft.com/en-us/library/bb675178
@Pawel As far as I can tell it does not support type safety. Aka, can you do root.XPath("people/person[@Birth<={0}]", DateTime.Now.AddYears(-30));?
Linq to Xml was introduced in .NET Framework 3.5. I don't have VS2008 installed anymore to check but I am pretty much sure it worked there as well - here is a link I posted before but it applied to .NET Framework 3.5. msdn.microsoft.com/en-us/library/bb675178(v=vs.90). I am not really sure what you mean but type safety but you can use string.Format to prevent from XPath injection attacks. Also, Linq to Xml provides explicit conversion operators for XElement and XAttribute (e.g. msdn.microsoft.com/en-us/library/system.xml.linq.xattribute) so you can just cast to be strongly typed
That is what the library does, is convert and compare based on the type provided, automatically for you.
@Pawel Thx Pawel, I adjusted the answer to include the Linq-to-Xml version, as theirs doesn't require the use of the library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.