Read an XML file

  • Thread starter Thread starter digitalshehan
  • Start date Start date
D

digitalshehan

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!
 
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....
 
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);
 
Thanks Morten!

Appreciate the help... :)


Morten said:
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,

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!
 
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 said:
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,

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!
 
Because, the original XML did NOT have an Employees root, but many
Employee roots.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top