Loop reading xml

A

Alberto

I'm trying to read a xml file who is like this:
<frase>
<nodo>
<x>10</x>
<y>20</y>
<text>aaa</text>
</nodo>
<nodo>
<x>50</x>
<y>60</y>
<text>bbb</text>
</nodo>
</frase>

I'd like to read the values of every "nodo" in order to create a class with
the values "x", "y" and "text" but I don't know how to do it. The code I
have now is this. I really appreciate that you tell me how to change it to
read the values. I suppose that I have to write a loop to read the values of
every value. Thank you very much.


FileStream fs = new FileStream(fileName, FileMode.Open);
XmlTextReader tr = new XmlTextReader(fs);
while(!tr.EOF)
{
if (tr.MoveToContent() == XmlNodeType.Element)
....
else
tr.Read();
}
 
M

Martin Honnen

Alberto said:
I'm trying to read a xml file who is like this:
<frase>
<nodo>
<x>10</x>
<y>20</y>
<text>aaa</text>
</nodo>
<nodo>
<x>50</x>
<y>60</y>
<text>bbb</text>
</nodo>
</frase>

I'd like to read the values of every "nodo" in order to create a class with
the values "x", "y" and "text" but I don't know how to do it. The code I
have now is this. I really appreciate that you tell me how to change it to
read the values. I suppose that I have to write a loop to read the values of
every value. Thank you very much.

I wouldn't bother with Xml(Text)Reader which is a low level API. With
..NET 2.0 you can use XPath 1.0 with
System.Xml.XPathDocument/XPathNavigator if you need read-only access or
System.Xml.XmlDocument if you also want to manipulate the XML.

With .NET 3.5 you can use LINQ to XML e.g.

XDocument doc = XDocument.Load(fileName);
IEnumerable<Foo> foos =
from nodo in doc.Root.Elements("nodo")
select new Foo() {
X = (int)nodo.Element("x"),
Y = (int)nodo.Element("y")
Text = (string)nodo.Element("text")
};

That assumes you have e.g.

public class Foo
{
public int X { get; set; }
public int Y { get; set; }
public string Text { get; set; }
}

And if you are not familiar with XPath or LINQ and you want to avoid
learning that then you could consider XML deserialization with
System.Xml.Serialization.XmlSerializer.
The xsd.exe tool in the .NET framework SDK can first infer an XML schema
from your XML document, then infer .NET class code representing that
schema and then you can use those classes with XmlSerializer.

All those APIs use XmlReader under the hood but you don't need to use it
explicitly unless you are dealing with very large XML documents where
the other APIs run into memory problems.
 
P

Peter Duniho

Alberto said:
I'm trying to read a xml file who is like this:
<frase>
<nodo>
<x>10</x>
<y>20</y>
<text>aaa</text>
</nodo>
<nodo>
<x>50</x>
<y>60</y>
<text>bbb</text>
</nodo>
</frase>

I'd like to read the values of every "nodo" in order to create a class with
the values "x", "y" and "text" but I don't know how to do it. The code I
have now is this. I really appreciate that you tell me how to change it to
read the values. I suppose that I have to write a loop to read the values of
every value. Thank you very much. [...]

Look at the System.Xml.Linq namespace. There are code examples in the
documentation, and the classes allow for a higher-level (i.e.
object-based) representation of your XML document that is more
convenient to use (you can even use XPath expressions).

Pete
 
A

Alberto

Could you tell me where to find an example to manage a xml file like this:

<frase>
<nodo>
<x>10</x>
<y>20</y>
<text>aaa</text>
<hijos>
<hijo>1</hijo>
<hijo>2</hijo>
<hijo>3</hijo>
</hijos>
</nodo>
<nodo>
<x>50</x>
<y>60</y>
<text>bbb</text>
<hijos/>
</nodo>
</frase>

Thank you
Peter Duniho said:
Alberto said:
I'm trying to read a xml file who is like this:
<frase>
<nodo>
<x>10</x>
<y>20</y>
<text>aaa</text>
</nodo>
<nodo>
<x>50</x>
<y>60</y>
<text>bbb</text>
</nodo>
</frase>

I'd like to read the values of every "nodo" in order to create a class
with the values "x", "y" and "text" but I don't know how to do it. The
code I have now is this. I really appreciate that you tell me how to
change it to read the values. I suppose that I have to write a loop to
read the values of every value. Thank you very much. [...]

Look at the System.Xml.Linq namespace. There are code examples in the
documentation, and the classes allow for a higher-level (i.e.
object-based) representation of your XML document that is more convenient
to use (you can even use XPath expressions).

Pete
 
M

Martin Honnen

Alberto said:
Could you tell me where to find an example to manage a xml file like this:

<frase>
<nodo>
<x>10</x>
<y>20</y>
<text>aaa</text>
<hijos>
<hijo>1</hijo>
<hijo>2</hijo>
<hijo>3</hijo>
</hijos>
</nodo>
<nodo>
<x>50</x>
<y>60</y>
<text>bbb</text>
<hijos/>
</nodo>
</frase>

The LINQ to XML documentation is online here:
http://msdn.microsoft.com/en-us/library/bb387098.aspx

Your local MSDN/Visual Studio 2008 documentation also has that section.

As for that XML, expanding on my earlier sample

XDocument doc = XDocument.Load(fileName);
IEnumerable<Foo> foos =
from nodo in doc.Root.Elements("nodo")
select new Foo() {
X = (int)nodo.Element("x"),
Y = (int)nodo.Element("y")
Text = (string)nodo.Element("text"),
Hijos = (from hijo in node.Element("hijos").Elements("hijo")
select (int)hijo).ToList()
};

That assumes you have e.g.

public class Foo
{
public int X { get; set; }
public int Y { get; set; }
public string Text { get; set; }
public List<int> Hijos { get; set; }
}
 
A

Alberto

Thank you. I'm still trying to adapt the code to may app but I'm shocked
with the power of this code.
 
K

kndg

Martin said:
[...]
The xsd.exe tool in the .NET framework SDK can first infer an XML schema
from your XML document, then infer .NET class code representing that
schema and then you can use those classes with XmlSerializer.

Wow, it is indeed a valuable tool.
Thanks Martin!
 

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