Linq to XML search

Searching thru xml doc to find an attribute

Page 1 of 1

6 Replies - 3764 Views - Last Post: 27 January 2010 - 05:15 PM Rate Topic: -----

#1 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Linq to XML search

Post icon  Posted 26 January 2010 - 06:46 PM

Good day all,
I have spent quite some time on this and have been scouring the net for help, but I couldn't find exactly the problem I'm having and thought someone might be able to steer me in the right direction.

Ok here is my problem - A user selects a value from a drop down box on a ASP.NET page, along with other data. When the user clicks next, I want to verify that the selection that the user has made exists in the xml file. I am doing this to prevent any unwanted sql injections etc. So essentially, if the value selected exists in the xml document, the page validates otherwise it displays an error. There are a number of xml documents in the application, but I have just posted one example and the the xml file.

<?xml version="1.0" encoding="utf-8" ?>
<titles>
  <title text="Mr" value="Mr"></title>
  <title text="Mrs" value="Mrs"></title>
  <title text="Miss" value="Miss"></title>
</titles>



 //reads thru the relevant xml file and checks to see if the value
	//selected by the applicant exists in the xml document
//where value is the selected value of the drop down box
//and source is the name of the xml file that contains the 
//data.
	private bool checkXML(String value, String source)
	{
		Boolean valid = false;

		//	string msg;
		
		String fileName = HttpContext.Current.Server.MapPath("/App_Data/XMLfiles/" + source + ".xml");

		XElement xml = XElement.Load(@fileName);
		var query = from c in xml.Elements()				
					select c;
		foreach (string i in query.Attributes())
		{
			if (i == value)
			{
				valid = true;
			}

		} return valid;
			 
		
	} 


Everything works up until the foreach, where it doesn't appear to be looping thru everything and it only returns the first value in the xml file.

Thanks in advance
Bec

Is This A Good Question/Topic? 0
  • +

Replies To: Linq to XML search

#2 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: Linq to XML search

Posted 26 January 2010 - 07:25 PM

Well, it's always the way, as soon as you post your problem you work it out :P

Anyway, I thought I would post the solution for those who were interested and for those who stumble across the same problem.

//reads thru the relevant xml file and checks to see if the value
	//selected by the applicant exists in the xml document
	private bool checkXML(String value, String source)
	{
		Boolean valid = false;

		//	string msg;
		
		String fileName = HttpContext.Current.Server.MapPath("/App_Data/XMLfiles/" + source + ".xml");

		XElement xml = XElement.Load(@fileName);
		var query = from c in xml.Elements()
					where c.Attribute("text").Value == value
					select c;

		if (query.Count() > 0)
		{
			valid = true;
		}
		return valid;
			 
		
	} 



Altho, I still don't really understand why this works I just know that it works. If you can perhaps explain why I would be very grateful.
Cheers
Bec
Was This Post Helpful? 0
  • +
  • -

#3 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: Linq to XML search

Posted 26 January 2010 - 07:30 PM

I think it would be better to do the following:

var query = for c in xml.Root.Elements()
				  where c.Attribute("text").Value == value && c.Attribute("value").Value == value
				  select c;



That, or something similar, should give you all the matching results in an IEnumerable<T> without having to use the foreach loop to verify.

Edit: I am glad you were able to find the solution quickly.

This post has been edited by DuoShin85: 26 January 2010 - 07:33 PM

Was This Post Helpful? 1
  • +
  • -

#4 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: Linq to XML search

Posted 26 January 2010 - 07:55 PM

Thanks for that, I will give it a try
Cheers
Bec
Was This Post Helpful? 0
  • +
  • -

#5 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Linq to XML search

Posted 27 January 2010 - 02:08 AM

View Postbecstribe, on 26 Jan, 2010 - 06:25 PM, said:

XElement xml = XElement.Load(@fileName);
var query = from c in xml.Elements()
		  where c.Attribute("text").Value == value
		  select c;



Altho, I still don't really understand why this works I just know that it works. If you can perhaps explain why I would be very grateful.
Cheers
Bec


What you are doing here with this linq statement is telling the compiler to look through your XElement (containing the XML file)at all the elements in it (using from c in xml.Elements()) and find the ones where that elements attribute named "text" is equal to "value". If that is true it takes that current element and adds it to an IEnumerable<T>. If I am not mistaken var is actually going to be the same as IEnumerable<XElement> in this case. Hope this helps you understand it.
Was This Post Helpful? 0
  • +
  • -

#6 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: Linq to XML search

Posted 27 January 2010 - 10:08 AM

@Adkins: You are right var is an implicit type, so it will be treated as IEnumerable<T>.

This post has been edited by DuoShin85: 27 January 2010 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Re: Linq to XML search

Posted 27 January 2010 - 05:15 PM

Thanks so much for your help, very much appreciated.
Yeah, I understand it now too. There is just soooooo much to know and learn when you are programming.

Cheers
Bec
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1