Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have a problem. I´m searching a xml-document for a specific attribute value.

This is my code:

XElement root = XElement.Load(System.Environment.CurrentDirectory + "\\Documents\\Codec\\super.xml");
//Search the Name of the Signal in the VAR-Region of the super.xml file. Show all ancestor elements of type PACKET and of this the first attribute ("name")

var result = (from t in root.Descendants("VAR") where (string)t.Attribute("name") == textBox1.Text select t.Ancestors("PACKET").Attributes().First().Value).ToList();


This works great and I get what I want, but I can´t perform a "case-insensitive" search!
So the users of my code have to be aware of the correct case-sensitive name of the searched value.
Some searched values are: "DcdeF1234" so the user has to type exactly that value into the search-Textbox. This is not very user friendly. It would be better if the user could write : "dcdef1234".


I tried different pieces of code found on the net, but without success.

For Example:

SQL
where d.Element("name").Value.IndexOf(
  textBox1.Text, StringComparison.InvariantCultureIgnoreCase) > 0


--> This works, but I get a null-reference-exception, if the search value is not found!

regards

Adam
Posted
Updated 17-Feb-14 22:58pm
v2
Comments
Maarten Kools 18-Feb-14 5:07am    
XML is case sensitive, if you have XML with mixed case I would suggest to clean that up and use a specific naming convention. Of course there are ways around it, instead of doing d.Element("name") do something like d.Elements().Where(e => String.Equals(e.Name.LocalName, "name", StringComparison.OrdinalIgnoreCase).FirstOrDefault().

As for searching values of elements, you can do case insensitive comparisons of course. If you're getting a NullReferenceException on that one line, it means either d is null, or there is no element called name (note the lower case), or Value is null (or event textBox1 can be null). Only by debugging it you will be able to tell what exactly is null here.
Member 8857897 18-Feb-14 7:49am    
Hi!

I think VALUE is null, because there is no value for the searched value and so it gets null.
But how can I handle this problem?

var result = (from t in root.Descendants("VAR") where t.Attribute("name").-->Value<--. IndexOf(textBox1.Text, StringComparison.InvariantCultureIgnoreCase) > 0 select t.Ancestors("PACKET").Attributes().First().Value).ToList();

1 solution

Since value is null, I would do something like this:
C#
var result = (from t in root.Descendants("VAR") 
                let nameAttr = t.Attribute("name") 
                where nameAttr != null && nameAttr.Value != null // Either the attribute is missing, or the value is missing, skip in both cases
                where String.Equals(nameAttr.Value, textBox1.Text, StringComparison.OrginalIgnoreCase)
                select t.Ancestors("PACKET").Attributes().First().Value)
            .ToList();
 
Share this answer
 
Comments
Member 8857897 18-Feb-14 8:13am    
Hi! This works perfectly!
GREAT, THANK YOU VERY VERY MUCH!

Adam
Maarten Kools 18-Feb-14 8:14am    
Glad to help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900