I Love Stackoverflow
I Love Stackoverflow

Reputation: 6868

Filter XMLNodeList using xPath & Wildcard characters

I have "XML" as below:

<ParentNode>
    <ChildNode id="1" Display_Name="ABC"/>
    <ChildNode id="2" Display_Name="DEF"/>
    <ChildNode id="3" Display_Name="DAX"/>
    <ChildNode id="4" Display_Name="LAM"/>
    <ChildNode id="5" Display_Name="PKR"/>
    <ChildNode id="6" Display_Name="UYA"/>
</ParentNode>

I want to get list of all the Nodes in XMLNodeList in C# using xPath having "A" [regardless of capitals or small] in Display_Name attribute.

What I've tried is:

root.SelectNodes("descendant-or-self::*[contains(@DISPLAY_NAME,'end')]")

Here, root is containing my XML and it is an object of XMLDocument.

Also, how can I make this filter by ignoring either Display_Name is in small letters or capital letters.

Upvotes: 0

Views: 1841

Answers (3)

Irfan
Irfan

Reputation: 1

Use OuterXml method.

Try this:

//Load Data     
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);

//Go the xPath
XmlNode titleNode = xmlDoc.SelectSingleNode(xPath);

//Get the OutXml (You dont need to use a new variable)
string nodeValue = titleNode.OuterXml;

//Load this string as a new XmlDocument and use the second xPath
XmlDocument xmlDoc2 = new XmlDocument();
xmlDoc2.LoadXml(nodeValue);
titleNode = xmlDoc.SelectSingleNode(xPath2);

Upvotes: 0

Shubham Jain
Shubham Jain

Reputation: 17593

Try with below XPath

/ParentNode/ChildNode/@Display_Name

To get result for both

Above XPath will return you all results of ChildNode. Now iterate this XPath to extract all results

Hope it will help you :)

Upvotes: 0

har07
har07

Reputation: 89325

"I want to get list of all the Nodes in XMLNodeList in C# using xPath having "A" [regardless of capitals or small] in Display_Name attribute. "

Nature of XML and XPath is case-sensitive. There is no pretty way to do case-insensitive matching using XPath (at least in XPath 1.0, version that is supported by .NET). One known way is using translate() to convert Display_Name value to lower-case before doing further comparison, something like this (see related post) :

var xpath = @"//*[
                contains(
                    translate(@Display_Name
                              ,'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                              ,'abcdefghijklmnopqrstuvwxyz'
                    )
                    ,'a'
                )
            ]";
var result = root.SelectNodes(xpath);

Upvotes: 1

Related Questions