473,405 Members | 2,421 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,405 software developers and data experts.

Read XML File

It must be simple, but I don't find how I can read a XmlFile in an
ArrayList.

Sample of my XmlFile:

<?xml version="1.0" encoding="utf-8" ?>
<Relations>
<Person>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Person>
<Person>
<FirstName>Peter</FirstName>
<LastName>Clinton</LastName>
</Person>
</Relations>

Thanks for your help or link to an example.
Gerrit
--

www.gsnel.nl
Oct 23 '06 #1
4 16665
Gerrit,

1. Create a XmlDocument.
2. Use LoadXml() on your instance to load your XML. You can also use
Load(), depending on where your XML is coming from.
3. SelectNodes() with an XPath expression (try Relations/Person) to get
back an XmlNodeList.
4. Parse each member node into whatever you want in your list and add
it to your ArrayList.

You may find it cleaner to create IXmlSerializable Person and Relations
objects that deserialize themselves from an XmlReader.

Stephan

Gerrit wrote:
It must be simple, but I don't find how I can read a XmlFile in an
ArrayList.

Sample of my XmlFile:

<?xml version="1.0" encoding="utf-8" ?>
<Relations>
<Person>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Person>
<Person>
<FirstName>Peter</FirstName>
<LastName>Clinton</LastName>
</Person>
</Relations>

Thanks for your help or link to an example.
Gerrit
--

www.gsnel.nl
Oct 23 '06 #2
<ss*****@gmail.comschreef in bericht
news:11**********************@m7g2000cwm.googlegro ups.com...
Gerrit,

1. Create a XmlDocument.
2. Use LoadXml() on your instance to load your XML. You can also use
Load(), depending on where your XML is coming from.
3. SelectNodes() with an XPath expression (try Relations/Person) to get
back an XmlNodeList.
4. Parse each member node into whatever you want in your list and add
it to your ArrayList.

You may find it cleaner to create IXmlSerializable Person and Relations
objects that deserialize themselves from an XmlReader.

Stephan
Thanks for your answer, but..

I have read more of this kind of explanation, but my English is not so good
and I am new in C# and XML so it is a little abacadabra for me. So I am
looking for a piece of code what do what you mean.

Gerrit
Oct 23 '06 #3
On Tue, 24 Oct 2006 01:07:31 +0200, Gerrit <gs*************@hotmail.com>
wrote:
<ss*****@gmail.comschreef in bericht
news:11**********************@m7g2000cwm.googlegro ups.com...
>Gerrit,

1. Create a XmlDocument.
2. Use LoadXml() on your instance to load your XML. You can also use
Load(), depending on where your XML is coming from.
3. SelectNodes() with an XPath expression (try Relations/Person) to get
back an XmlNodeList.
4. Parse each member node into whatever you want in your list and add
it to your ArrayList.

You may find it cleaner to create IXmlSerializable Person and Relations
objects that deserialize themselves from an XmlReader.

Stephan

Thanks for your answer, but..

I have read more of this kind of explanation, but my English is not so
good
and I am new in C# and XML so it is a little abacadabra for me. So I am
looking for a piece of code what do what you mean.

Gerrit

Hi Gerrit,

What do you mean by read xml in arraylist. If you just want to store the
xml to an arraylist then this code would store it.

string xml = "<..../>"; // somexml
ArrayList list = new ArrayList();
list.Add(xml);

If you want to store some information contained inside the xml into an
arraylist, then you need to parse the xml, and you can do that using
XmlDocument and SelectNodes/SelectSingleNode etc

Using your xml, you can store the names in an ArrayList like this (I'll
assume the xml is stored in a string named xml)

<?xml version="1.0" encoding="utf-8" ?>
<Relations>
<Person>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Person>
<Person>
<FirstName>Peter</FirstName>
<LastName>Clinton</LastName>
</Person>
</Relations>
ArrayList list = new ArrayList(); // or List<stringlist=
new List<string>();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNodeList nodes = doc.SelectNodes("Relations/Person");
foreach (XmlNode node in nodes)
{
XmlNode firstNameNode = node.SelectSingleNode("FirstName");
XmlNode lastNameNode = node.SelectSingleNode("LastName");
list.Add(firstNameNode.InnerText + " "
+ lastNameNode.InnerText);
}

foreach (string s in list)
Console.WriteLine(s);

The result will be:
John Smith
Peter Clinton

What the code does is store the xml inside an XmlDocument. Once inside
you can parse the data in a number of ways.
doc.SelectNodes will return a list of nodes using an XPath query
"Relations/Person". Standing at the root node (doc) it will return all
XmlNodes having a Relations node and a Person node that is a child node of
Relations. In your xml there are two Person nodes with a Relations node
as Parent so two nodes will returned.

For each node in the result do two more XPath queries using
SelectSingleNode. Since we are doing node.SelectSingleNode we are
"standing" on a Person node, so simply querying for "FirstName" or
"LastName" should return the respective nodes. Then it is just a matter
of reading the nodes' InnerText to obtain the name.

For comparison

XmlNodeList nodes =
doc.SelectNodes("Relations/Person/LastName");
foreach (XmlNode node in nodes)
{
list.Add(node.InnerText);
}

will return two nodes as well, if you only need the last names

The result will be:
Smith
Clinton

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 24 '06 #4
"Morten Wennevik" <Mo************@hotmail.comschreef in bericht
news:op***************@tr024.bouvet.no...
On Tue, 24 Oct 2006 01:07:31 +0200, Gerrit <gs*************@hotmail.com>
wrote:
<ss*****@gmail.comschreef in bericht
news:11**********************@m7g2000cwm.googlegro ups.com...
>Gerrit,

1. Create a XmlDocument.
2. Use LoadXml() on your instance to load your XML. You can also use
Load(), depending on where your XML is coming from.
3. SelectNodes() with an XPath expression (try Relations/Person) to get
back an XmlNodeList.
4. Parse each member node into whatever you want in your list and add
it to your ArrayList.

You may find it cleaner to create IXmlSerializable Person and Relations
objects that deserialize themselves from an XmlReader.

Stephan

Thanks for your answer, but..

I have read more of this kind of explanation, but my English is not so
good
and I am new in C# and XML so it is a little abacadabra for me. So I am
looking for a piece of code what do what you mean.

Gerrit

Hi Gerrit,

What do you mean by read xml in arraylist. If you just want to store the
xml to an arraylist then this code would store it.

string xml = "<..../>"; // somexml
ArrayList list = new ArrayList();
list.Add(xml);

If you want to store some information contained inside the xml into an
arraylist, then you need to parse the xml, and you can do that using
XmlDocument and SelectNodes/SelectSingleNode etc

Using your xml, you can store the names in an ArrayList like this (I'll
assume the xml is stored in a string named xml)

<?xml version="1.0" encoding="utf-8" ?>
<Relations>
<Person>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Person>
<Person>
<FirstName>Peter</FirstName>
<LastName>Clinton</LastName>
</Person>
</Relations>
ArrayList list = new ArrayList(); // or List<stringlist =
new List<string>();

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNodeList nodes = doc.SelectNodes("Relations/Person");
foreach (XmlNode node in nodes)
{
XmlNode firstNameNode = node.SelectSingleNode("FirstName");
XmlNode lastNameNode = node.SelectSingleNode("LastName");
list.Add(firstNameNode.InnerText + " "
+ lastNameNode.InnerText);
}

foreach (string s in list)
Console.WriteLine(s);

The result will be:
John Smith
Peter Clinton

What the code does is store the xml inside an XmlDocument. Once inside
you can parse the data in a number of ways.
doc.SelectNodes will return a list of nodes using an XPath query
"Relations/Person". Standing at the root node (doc) it will return all
XmlNodes having a Relations node and a Person node that is a child node of
Relations. In your xml there are two Person nodes with a Relations node
as Parent so two nodes will returned.

For each node in the result do two more XPath queries using
SelectSingleNode. Since we are doing node.SelectSingleNode we are
"standing" on a Person node, so simply querying for "FirstName" or
"LastName" should return the respective nodes. Then it is just a matter
of reading the nodes' InnerText to obtain the name.

For comparison

XmlNodeList nodes =
doc.SelectNodes("Relations/Person/LastName");
foreach (XmlNode node in nodes)
{
list.Add(node.InnerText);
}

will return two nodes as well, if you only need the last names

The result will be:
Smith
Clinton

--
Happy Coding!
Morten Wennevik [C# MVP]

Hallo Morten,

Thanks for your help. This was exactly where I was looking for.
And with your explanation, I understand what is is doing.

Gerrit

--

www.gsnel.nl
Oct 24 '06 #5

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

Similar topics

4
by: domtam | last post by:
Suppose I have one of those USB storage devices (like a mp3 player, USB thumbdrive, or even digital camera) connected to my computer. I'd like to write a C# program that can - detect that the...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
4
by: John please don't spam me! | last post by:
VB.Net 2003 Hi, 2 questions: 1. I want to read a file in without locking it, as it is Log file. 2. I want to be able to read from the last point it wa read upto. The project is: Search...
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
1
by: potluri040 | last post by:
hi, could any one let me know how to read file thru VB script. Scenerio is like this: i am keeping a personal details such as name, age, sex, company, location in a text file called user details...
1
by: samira | last post by:
hi all, i built aproject to my client he need that the visitors to the site can read file from his computer, how can i do this , we uuse asp.net over c#
3
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post by:
Hi, Is there anything wrong with the code below in sending my browser page to Excel? Before my page opens in Excel there's a message "Problems came up in the following areas during load:" and it...
2
by: xplode144 | last post by:
I have a Web application. i need to read a file once during the startup and preserve the read data throughout the life of the application. i will to access the data often during the page_load of...
2
by: danimian | last post by:
Hello, first i am creating xml file if file does not exist. String myFile = "C:\myxmlfile.xml"; if (!File.Exists(myFile)) { using (FileStream conStream = new FileStream(myFile,...
0
by: leeamiin | last post by:
Hi, i need help with bellow file format, i work for telecom company and i'm the developer, what i need help with is to read file with bellow format, as you can see the file has { and , as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.