473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Read an XML file

Hi All,

I have an xml file containing the following:
<?xml version="1.0" encoding="utf-8"?>
<Employee name="john" />
<Employee name="peter" />
<Employee name="mark" />

Can anyone let me know of away to read values from the xml file and add
them to an arraylist...

Thanks!

Aug 30 '06 #1
6 3226
just to add...

This is what i'm currently using:

XmlTextReader reader = new XmlTextReader(_fileName);
reader.WhitespaceHandling = WhitespaceHandling.None;

while(reader.Name != "Employee" && !reader.EOF)
reader.Read();

while(reader.Name == "Employee")
{
_shareCollection.Add(reader["name"]);
}

reader.Close();

Unfortunately, control never comes out of the while loop....

Aug 30 '06 #2
Hi,

Well, first, this is not valid XML as it has multiple root elements. It
should be something like this instead

<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee name="john" />
<Employee name="peter" />
<Employee name="mark" />
</Employees>

You can then load it into an XmlDocument and use SelectNodes to grab the
Employees, and then read the name attribute

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Employees.xml");

XmlNodeList elements = doc.SelectNodes("Employees/Employee");

ArrayList list = new ArrayList();

foreach (XmlNode node in elements)
list.Add(node.Attributes["name"].Value);


On Wed, 30 Aug 2006 08:29:39 +0200, <di***********@gmail.comwrote:
Hi All,

I have an xml file containing the following:
<?xml version="1.0" encoding="utf-8"?>
<Employee name="john" />
<Employee name="peter" />
<Employee name="mark" />

Can anyone let me know of away to read values from the xml file and add
them to an arraylist...

Thanks!


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 30 '06 #3
Thanks Morten!

Appreciate the help... :)
Morten Wennevik wrote:
Hi,

Well, first, this is not valid XML as it has multiple root elements. It
should be something like this instead

<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee name="john" />
<Employee name="peter" />
<Employee name="mark" />
</Employees>

You can then load it into an XmlDocument and use SelectNodes to grab the
Employees, and then read the name attribute

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Employees.xml");

XmlNodeList elements = doc.SelectNodes("Employees/Employee");

ArrayList list = new ArrayList();

foreach (XmlNode node in elements)
list.Add(node.Attributes["name"].Value);


On Wed, 30 Aug 2006 08:29:39 +0200, <di***********@gmail.comwrote:
Hi All,

I have an xml file containing the following:
<?xml version="1.0" encoding="utf-8"?>
<Employee name="john" />
<Employee name="peter" />
<Employee name="mark" />

Can anyone let me know of away to read values from the xml file and add
them to an arraylist...

Thanks!

--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 30 '06 #4
Morten,
Why is this invalid XML? I see an Employees root, and then multiple Employee
elements each with a name attribute. If I save this as "Test.xml" and load it
in Internet Exploder, it parses just fine.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Morten Wennevik" wrote:
Hi,

Well, first, this is not valid XML as it has multiple root elements. It
should be something like this instead

<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee name="john" />
<Employee name="peter" />
<Employee name="mark" />
</Employees>

You can then load it into an XmlDocument and use SelectNodes to grab the
Employees, and then read the name attribute

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Employees.xml");

XmlNodeList elements = doc.SelectNodes("Employees/Employee");

ArrayList list = new ArrayList();

foreach (XmlNode node in elements)
list.Add(node.Attributes["name"].Value);


On Wed, 30 Aug 2006 08:29:39 +0200, <di***********@gmail.comwrote:
Hi All,

I have an xml file containing the following:
<?xml version="1.0" encoding="utf-8"?>
<Employee name="john" />
<Employee name="peter" />
<Employee name="mark" />

Can anyone let me know of away to read values from the xml file and add
them to an arraylist...

Thanks!

--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 30 '06 #5
Because, the original XML did NOT have an Employees root, but many
Employee roots.
On Wed, 30 Aug 2006 13:56:02 +0200, Peter Bromberg [C# MVP]
<pb*******@yahoo.nospammin.comwrote:
Morten,
Why is this invalid XML? I see an Employees root, and then multiple
Employee
elements each with a name attribute. If I save this as "Test.xml" and
load it
in Internet Exploder, it parses just fine.

Peter


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 30 '06 #6
Sorry,
It must have been your corrected version I was looking at!
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Morten Wennevik" wrote:
Because, the original XML did NOT have an Employees root, but many
Employee roots.
On Wed, 30 Aug 2006 13:56:02 +0200, Peter Bromberg [C# MVP]
<pb*******@yahoo.nospammin.comwrote:
Morten,
Why is this invalid XML? I see an Employees root, and then multiple
Employee
elements each with a name attribute. If I save this as "Test.xml" and
load it
in Internet Exploder, it parses just fine.

Peter

--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.