Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Read an XML file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
digitalshehan@gmail.com - 30 Aug 2006 07:29 GMT
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!
digitalshehan@gmail.com - 30 Aug 2006 07:42 GMT
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....
Morten Wennevik - 30 Aug 2006 07:49 GMT
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);

> Hi All,
>
[quoted text clipped - 8 lines]
>
> Thanks!

Signature

Happy Coding!
Morten Wennevik [C# MVP]

digitalshehan@gmail.com - 30 Aug 2006 08:27 GMT
Thanks Morten!

Appreciate the help... :)

> Hi,
>
[quoted text clipped - 33 lines]
> >
> > Thanks!
Peter Bromberg [C# MVP] - 30 Aug 2006 12:56 GMT
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

Signature

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

> Hi,
>
[quoted text clipped - 33 lines]
> >
> > Thanks!
Morten Wennevik - 30 Aug 2006 13:00 GMT
Because, the original XML did NOT have an Employees root, but many  
Employee roots.

> Morten,
> Why is this invalid XML? I see an Employees root, and then multiple  
[quoted text clipped - 4 lines]
>
> Peter

Signature

Happy Coding!
Morten Wennevik [C# MVP]

Peter Bromberg [C# MVP] - 30 Aug 2006 19:19 GMT
Sorry,
It must have been your corrected version I was looking at!
Peter

Signature

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

> Because, the original XML did NOT have an Employees root, but many  
> Employee roots.
[quoted text clipped - 7 lines]
> >
> > Peter

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.