Read XML File

G

Gerrit

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
 
S

ssamuel

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
 
G

Gerrit

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
 
M

Morten Wennevik

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<string> list=
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
 
G

Gerrit

"Morten Wennevik" <[email protected]> schreef in bericht
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<string> list =
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
 

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

Top