By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,112 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,112 IT Pros & Developers. It's quick & easy.

Read XML File

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
<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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.