Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare
Reading XML Element to compare Reading XML Element to compare
Reading XML Element to compare
Go Back  Xtreme Visual Basic Talk > > > Reading XML Element to compare


Reply
 
Thread Tools Display Modes
  #1  
Old 07-17-2012, 11:54 AM
AmazingTrans AmazingTrans is offline
Newcomer
 
Join Date: Nov 2005
Posts: 10
Default Reading XML Element to compare


Hi everybody,

I am trying to read an xml file to see if it contains a specific element and also retrieve its value. For example:
1. I want to check if the xml file contains Akita Inu, and increase variable "i" for every one it finds.
2. Add the values for every akita inu it finds. for eg: i = 2 + 3 (I wish to implement this but do not know how to get the read values of it.

What I have now is the objText extracts only "Element, Name="p"". And I just wish to extract the names of the dogs only.
Hope to hear from someone.

XML FILE
Code:
<?xml version="1.0" standalone="no"?>
<Animal ID="Dogs" LastSaveDateTime="2012-07-17 12:13:41" Index="0">
<p name="Affenpinscher">5</p>
<p name="Aidi">2</p>
<p name="Airedale Terrier">1</p>
<p name="Akita Inu">2</p>
<p name="Alaskan Klee Kai">2</p>
<p name="Alpine Spaniel">54</p>
<p name="Basset Hound">15</p>
<p name="Aidi">2</p>
<p name="Akita Inu">3</p>
VB . Net Code
Code:
Dim objText As XmlTextReader = Nothing
Dim numberOfAkita As Integer = 0
Dim i as Integer

Dim strFile As String

For Each strFile in c:\animal.xml
	objText = Nothing
	objText = New XmlTextReader(strFile)
	objText.WhitespaceHandling = WhitespaceHandling.None
	objText.MoveToContent()
	
	While (objText.Read())
		If (objText.Name = "Akita Inu") Then
			numberOfAkita = numberOfAkita  +1
		End If
	End While
	objText.Close()
Next
objText = Nothing

Last edited by AmazingTrans; 07-17-2012 at 03:19 PM.
Reply With Quote
  #2  
Old 07-17-2012, 05:23 PM
PlausiblyDamp's Avatar
PlausiblyDampReading XML Element to compare PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

What version of VB are you using? If it is .Net 3.0 or later then it might be easier to use the XLinq support to do this kind of thing.

If you are on a suitable version I can try to knock something together to give you an idea of what would be involved.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 07-19-2012, 09:20 AM
AmazingTrans AmazingTrans is offline
Newcomer
 
Join Date: Nov 2005
Posts: 10
Default

PlausiblyDamp,

I'm using .net '08.
Is XLinq faster than the XMLTextReader?

I have thousands of data here, and would like to process it.
Hope to hear from you.

Thanks!
Reply With Quote
  #4  
Old 07-22-2012, 02:57 PM
PlausiblyDamp's Avatar
PlausiblyDampReading XML Element to compare PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

First off if you check the documentation for XmlTextReader (http://msdn.microsoft.com/en-us/libr...extreader.aspx) it is recommended you don't create one directly but instead use XmlReader.Create to obtain one....

Regarding the performance of XLinq vs XmlTextReader though it doesn't seem to be clear cut in all scenarios, googling for comparison results seem to indicate XmlTextReader is generally the fastest but results is more code that is often harder to read and maintain, the performance benefits seem to lessen as the size of the document increases as well.

The Xlinq should look something like
Code:
Dim doc = XDocument.Load("c:\tmp\test.xml")

Dim x = Aggregate node In doc.Element("Animal").Elements("p")
	Where (node.Attribute("name").Value = "Akita Inu")
	Into Count = Count(), Total = Sum(CInt(node.Value))

Debug.WriteLine("Count {0}, Total {1}", x.Count, x.Total)
Which effectively loads the xml document into an XDocument and then will perform an aggregate of all child elements of Animal that have the name p and contain an attribute called name with a value of "Akita Inu", this information would then have a count and a sum performed on it, the Debug.WriteLine simply displays these two values.

Not sure if this is supported in 2008 but just out of interest 2010 would have allowed a slightly cleaner XML like syntax of
Code:
Dim doc = XDocument.Load("c:\tmp\test.xml")

Dim x = Aggregate node In doc.<Animal>.<p>
    Where node.@name = "Akita Inu"
    Into Count = Count(), Total = Sum(CInt(node.Value))
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 07-25-2012, 09:23 AM
AmazingTrans AmazingTrans is offline
Newcomer
 
Join Date: Nov 2005
Posts: 10
Default

Thanks plausibly. unfortunately, Looks like 08 version does not have xlinq.
Reply With Quote
  #6  
Old 07-25-2012, 09:44 AM
PlausiblyDamp's Avatar
PlausiblyDampReading XML Element to compare PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

2008 does include XLinq, you might need to make sure you have it in the list of imports at the top of the file though.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 07-27-2012, 03:18 PM
AmazingTrans AmazingTrans is offline
Newcomer
 
Join Date: Nov 2005
Posts: 10
Default

PlausiblyDamp,

I tried this below and it does not have it.
Imports System.Xml.xLinq

But it has this though.
Imports System.Xml.Linq

I dont think they are the same right?
Reply With Quote
  #8  
Old 07-28-2012, 12:55 PM
PlausiblyDamp's Avatar
PlausiblyDampReading XML Element to compare PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #9  
Old 08-01-2012, 06:36 AM
shazywie71 shazywie71 is offline
Newcomer
 
Join Date: Aug 2012
Posts: 1
Default

Try Liquid XML Viusal Basic Tool, the code will be much cleaner, its also got WYSIWYG intercae so much easier to see your child elements and even move them around via drag drop.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare Reading XML Element to compare
Reading XML Element to compare
Reading XML Element to compare
 
Reading XML Element to compare
Reading XML Element to compare
 
-->