Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

XML in C# 2005

I am new to XML.  I have added the System.XML namespace to my project.  I would like to know how to read this xml file and parse out each of the Authors and Titles.  I could do it with a streamreader, but would like to know the correct XML way to do it.

Thanks.

<?xml version="1.0" encoding="windows-1252"?>
<VirtualDJ_Database Version="521">
 <Song FilePath="F:\all djdekalb music\Neil Diamond - Red Red Wine.m4a" FileSize="7178860">
  <Display Author="Neil Diamond" Title="Red Red Wine" Album="Yngwie Malmsteen EP" Color="16639516" Cover="2" Tag="1" />
  <Infos SongLength="9956288" FirstSeen="810012222" FirstPlay="810022355" LastPlay="810062011" PlayCount="7" />
  <BPM Bpm="20330" Phase="975" />
  <FAME IsScanned="510" Volume="3375" Key="Em" />
  <Automix FadeStart="0" FadeEnd="9797632" RealStart="0" RealEnd="9949184" />
 </Song>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
 <Song FilePath="F:\all djdekalb music\Bryan Adams - (Everything I Do) I Do For You).m4a" FileSize="7967986">
  <Display Author="Bryan Adams" Title="(Everything I Do) I Do For You" Genre="Unclassifiable" Album="DJ Tools Volume 8" Year="1995" Color="5042923" Cover="2" Tag="1" />
  <Infos SongLength="10979264" FirstSeen="810012222" FirstPlay="810051633" LastPlay="810051633" PlayCount="1" />
  <BPM Bpm="20160" Phase="5938" />
  <FAME IsScanned="510" Volume="5137" Key="C#m" />
  <Automix FadeStart="188416" FadeEnd="10190848" RealStart="20480" RealEnd="10911744" />
 </Song>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
 <Song FilePath="F:\all djdekalb music\Bryan Adams - Everything I Do.m4a" FileSize="12744153">
  <Display Author="Bryan Adams" Title="Everything I Do" Color="6351405" Cover="34" Tag="1" />
  <Infos SongLength="17444800" FirstSeen="810012222" FirstPlay="810062005" LastPlay="810062005" PlayCount="1" />
  <BPM Bpm="20160" Phase="7272" />
  <FAME IsScanned="510" Volume="6816" Key="C#m" />
  <Automix MixType="1" CutStart="3554176" CutEnd="16215936" FadeStart="8192" FadeEnd="16539648" RealStart="0" RealEnd="17285120" />
 </Song>
 <Song FilePath="F:\all djdekalb music\Cheap Trick - I Want You To Want Me.m4a" FileSize="7361637">
  <Display Author="Cheap Trick" Title="I Want You To Want Me" Album="The Authorized Greatest Hits" Year="2000" Color="6339053" Cover="162" Tag="1" />
  <Infos SongLength="9905088" FirstSeen="810012222" FirstPlay="810012227" LastPlay="810062041" PlayCount="10" />
  <BPM Bpm="25654" Phase="10002" />
  <FAME IsScanned="510" Volume="6390" Key="Am" />
  <Automix MixType="1" TempoStart="786718" CutStart="785600" CutEnd="9271072" FadeStart="126976" FadeEnd="9900032" RealStart="32768" RealEnd="9900032" />
  <Cue Num="0" Name="Cue 1" Pos="1374208" />
 </Song>
</VirtualDJ_Database>
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also just use XmlTextReader and just pull the Title and Author attributes.  It has the advantage that you don't read the whole file in at once.  So if the XML file is large, the memory footprint will be small.  

It will also be a few seconds faster because you only parse the XML file once (in O notation, it is O(n)).  FernandoSoto's routine will effectively parse the XML twice [O(2n)].  If the XML file is small, the difference is negligible.
using (Stream StreamIn=new StreamReader(path)) {
	XmlTextReader dataIn=XmlTextReader.Create(StreamIn);
	while (dataIn.Read()) {
		string Author=dataIn.GetAttribute("Author");
		string Title=dataIn.GetAttribute("Title");
		if (!string.IsNullOrEmpty(Author) && !string.IsNullOrEmpty(Title)) {
			Console.WriteLine("Author = {0}\t\tTitle = {1}", Author, Title);
		}
	}
}

Open in new window

Avatar of g_johnson

ASKER

Fernando:  Your code worked perfectly.  Thank you.

Mogal:  I appreciate your help, but I couldn't get your code to run.  Some kind of casting problem on this line:  XmlTextReader dataIn=XmlTextReader.Create(StreamIn);

Being new to XML I also didn't understand your comments about reading the file once and/or twice.  A lesson for another day!  :-)
Thanks again
Not a problem, glad to help.  ;=)