Reading data from XML with XDocument

A

Alberto

In a xml file, I have a first element (appears only one time) with this
label:
<config>
<fuentepalabras>...</fuentepalabras>
<fuenteelementos>...</fuenteelementos>
</config>

After this first element, there are several called <elemento>:
<elemento>
...
</elemento>
....
<elemento>
...
</elemento>

I'm reading this elements with this code:

IEnumerable<Elemento> listaElementos;

XDocument doc = XDocument.Load(Nombre);

listaElementos =

from elemento in doc.Root.Elements("elemento")

select new Elemento()

{

Location = new Point((int)elemento.Element("x"),
(int)elemento.Element("y")),

Id = (int)elemento.Element("id"),

Text = (string)elemento.Element("texto"),

ListaHijos = (from hijo in elemento.Element("hijos").Elements("hijo")

select (int)hijo).ToList(),

ListaPadres = (from padre in elemento.Element("padres").Elements("padre")

select (int)padre).ToList()

};



My questión is how can I read the first element called <elemento>. I don't
know how to work with the XDocument. Thank you very much
 
M

Martin Honnen

Alberto said:
In a xml file, I have a first element (appears only one time) with this
label:
<config>
<fuentepalabras>...</fuentepalabras>
<fuenteelementos>...</fuenteelementos>
</config>

After this first element, there are several called <elemento>:
<elemento>
...
</elemento>
...
<elemento>
...
</elemento>

I'm reading this elements with this code:

IEnumerable<Elemento> listaElementos;

XDocument doc = XDocument.Load(Nombre);

listaElementos =

from elemento in doc.Root.Elements("elemento")

select new Elemento()

{

Location = new Point((int)elemento.Element("x"),
(int)elemento.Element("y")),

Id = (int)elemento.Element("id"),

Text = (string)elemento.Element("texto"),

ListaHijos = (from hijo in elemento.Element("hijos").Elements("hijo")

select (int)hijo).ToList(),

ListaPadres = (from padre in elemento.Element("padres").Elements("padre")

select (int)padre).ToList()

};



My questión is how can I read the first element called <elemento>. I don't
know how to work with the XDocument. Thank you very much

Well if you want the first XElement then it is simply
XElement firstElementoElement =
doc.Root.Elements("elemento").FirstOrDefault();

If you rather want to access the first Elemento object your above query
returns then simply use
Elemento firstElemento = listaElementos.FirstOrDefault();
 
A

Alberto

Sorry, probably I didn't explain what I want.
I really want to read the values who are inside the <config> element (only
appear once).

Thank you
 
P

Peter Duniho

Alberto said:
Sorry, probably I didn't explain what I want.
I really want to read the values who are inside the <config> element (only
appear once).

Unfortunately, your original post is not very clear about the structure
of the XML document. In particular, does an "elemento" element also
exist as a child of the "config" element? Or is it elsewhere? And what
is the name of the root element? Is it "config"? Or is the "config"
element contained within another element?

As far as the elements that are _not_ "elemento" (e.g. "fuentepalabras",
"fuenteelementos", etc.), you can access them just as you access the
child elements of the "elemento" element: with the Element() method.

Assuming that "config" is your root element, then just as you wrote
'elemento.Element("x")', you can also write
'doc.Root.Element("fuentepalabras")'.

If that doesn't seem to help or address your question, you need to take
more care to explain your question better. And of course, you
definitely shouldn't ask "how can I read the first element called
<elemento>" if what you really want to know is how "to read the values
who...only appear once" (given that it seems from your original post
that "elemento" elements appear more than once and thus are specifically
_not_ the ones you want to read, even though you specifically asked
about reading those.


Pete
 
A

Alberto

Sorry, what I want to read is "fuentepalabras", "fuenteelementos". This
elements appear only once. They are inside the "config" element who also
appear once. The elements called "elemento" appear several times and I don't
have problems reading them.

The complete structure of the xml is this:
<frase>
<config>
<fuentepalabras>...</fuentepalabras>
<fuenteelementos>...</fuenteelementos>
</config>
<elemento>
...
</elemento>
...
<elemento>
...
</elemento>
</frase>
 
P

Peter Duniho

Alberto said:
Sorry, what I want to read is "fuentepalabras", "fuenteelementos". This
elements appear only once. They are inside the "config" element who also
appear once. The elements called "elemento" appear several times and I don't
have problems reading them.

The System.Xml.Linq data structures represent the XML document as a tree
data structure. So, you can get those elements by traversing the data
structure programmatically:

XElement elementPalabras =
doc.Root.Element("config").Element("fuentepalabras");

Alternatively, you can use XPath:

XElement element =
doc.Root.XPathSelectElement("config/fuentepalabras");

Make sure to add the System.Linq.XPath namespace in your using
directives for the source code file where you want to use the method.

If you know for sure there is exactly one (for example) "fuentepalabras"
element in the entire document, then you can use the Descendants()
method instead:

XElement elementPalabras =
doc.Root.Descendants("fuentepalabras").First();

(The Descendants() method actually returns an enumeration of all
matching elements; if this could be empty, or if you could have more
than one somewhere in the document, then you'd need additional logic to
deal with those conditions).

There are probably a number of other ways to get at the particular node,
but hopefully the above should get you headed in the right direction.

Pete
 

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

Similar Threads


Top