XML and Xpath

Using VB.NET, I want obtain a collection of all xml elements within a file that have the same value for a certain attribute.   See XML below.  For example, I want all 'text' elements whose 'y' attribute is 292.969.  How do I do this?  

Thanks


-<font src="Referral - Rosenbeger.pdf_font1.ttf" face="CHVVWW+Helvetica-Bold" size="8">

<text height="9.217" width="33.455" y="277.219" x="20.25">FATHER</text>

<text height="9.217" width="23.372" y="277.219" x="103.5">RACE</text>

<text height="9.217" width="35.295" y="277.219" x="194.25">GENDER</text>

<text height="9.217" width="43.997" y="277.219" x="237">AGE (DOB)</text>

<text height="9.217" width="16.963" y="277.219" x="300">SSN</text>

<text height="9.217" width="29.337" y="277.219" x="324.75">PHONE</text>

<text height="9.217" width="40.334" y="277.219" x="381">ADDRESS</text>

</font>


-<font src="Referral - Anthony 639874.pdf_font2.ttf" face="RGIPLP+Helvetica" size="8">

<text height="9.217" width="4.588" y="292.969" x="20.25">2</text>

<text height="9.217" width="66.025" y="292.969" x="31.5">Jackson, Robert</text>

<text height="9.217" width="85.273" y="292.969" x="103.5">Black/African American</text>

</font>


-<font src="Referral - Anthony 639874.pdf_font2.ttf" face="RGIPLP+Helvetica" size="8" color="#A9A9A9">

<text height="9.217" width="5.958" y="308.719" x="111.002">N</text>

</font>


-<font src="Referral - Anthony 639874.pdf_font2.ttf" face="RGIPLP+Helvetica" size="8">

<text height="9.217" width="16.501" y="308.719" x="122.211"> Hisp</text>

<text height="9.217" width="17.879" y="292.969" x="194.25">Male</text>

<text height="9.217" width="58.245" y="292.969" x="237">25 (06/10/1989)</text>

<text height="9.217" width="221.812" y="292.969" x="324.75">267-111-2222 100 Main Street, XZY PA, 19000</text>

<text height="9.217" width="4.588" y="324.469" x="20.25">3</text>

<text height="9.217" width="63.279" y="324.469" x="31.5">Anthony, Michael</text>

<text height="9.217" width="85.273" y="324.469" x="103.5">Black/African American</text>

</font>
LVL 1
HLRosenbergerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi HLRosenberger;

Code like this should do what you need.

Imports System.Xml
Imports System.Xml.Linq

' Load a Well Formed XML document into memory             
Dim xdoc = XDocument.Load("C:\Working Directory\font.xml")
' The y value you want a collection of                    
Dim findYs As String = "292.969"                          
                                                          
' Find all matching elements                              
Dim textElements As List(Of XElement) =                   
    (From txt In xdoc.Descendants("text")                 
     Where txt.Attribute("y").Value = findYs              
     Select txt).ToList()                                 
                                                          
' Display the results                                     
For Each node As XElement In textElements                 
    Console.WriteLine(node.ToString())                    
Next

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HLRosenbergerAuthor Commented:
ok. Got it.  I also understand that XPATH can be used.  Do you is XPATH a better way to go?
it_saigeDeveloperCommented:
*No Points*

Proof of concept using Fernando's code -
Module Module1
	Sub Main()
		Dim document = XDocument.Load("Fonts.xml")

		Dim elements = (From descendant In document.Descendants("text")
		    Where descendant.Attribute("y").Value.Equals("292.969")
		    Select descendant)

		For Each element In elements
			Console.WriteLine(element)
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG-saige-
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Fernando SotoRetiredCommented:
Hi HLRosenberger;

To your last post, "Do you is XPATH a better way to go?", the selector for the node is simple and no need to use XPath in MHO.
Miguel OzSoftware EngineerCommented:
"Do you is XPATH a better way to go?",
It depends what are aiming for:
1) Code more readable and maintainable.  (LINQ - Fernando's answer is the way to go)
2) Performance. (Depending on how big the file,  for big files that you need read information then  XPathNavigator is the way to go)
HLRosenbergerAuthor Commented:
Thanksl
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.