Should i prefer XElement or XContainer?

K

K Viltersten

As far i understood the docs, i should use XElement
when i initialize the settings of my application from
the provided XML-file during start-up.

Now, i've got suggested (by R#) to use XContainer.
Is it a glich in the logic of the tool or was i simply
mistaken to use XElement?

The setup is as follows.

public Initalize(XElement params) {
// read the leafs/nodes and set the values
}
 
M

Martin Honnen

K said:
As far i understood the docs, i should use XElement
when i initialize the settings of my application from
the provided XML-file during start-up.

Now, i've got suggested (by R#) to use XContainer.
Is it a glich in the logic of the tool or was i simply
mistaken to use XElement?

The setup is as follows.

public Initalize(XElement params) {
// read the leafs/nodes and set the values
}

XElement and XDocument have methods like Parse or Load to parse XML.
XContainer does not have those methods so if you want to parse XML into
LINQ to XML nodes you have to use XElement or XDocument.
 
K

K Viltersten

As far i understood the docs, i should use XElement
XElement and XDocument have methods like Parse or Load to parse XML.
XContainer does not have those methods so if you want to parse XML into
LINQ to XML nodes you have to use XElement or XDocument.

Thanks for the reply. I'm taking it as a "no, don't change
to XContainer". Now, i'm going to be using LINQ to
anything any time soon, but still, i'd like to keep the
option opened, just in case.
 
H

Hans Kesting

K Viltersten formulated the question :
As far i understood the docs, i should use XElement
when i initialize the settings of my application from
the provided XML-file during start-up.

Now, i've got suggested (by R#) to use XContainer.
Is it a glich in the logic of the tool or was i simply
mistaken to use XElement?

The setup is as follows.

public Initalize(XElement params) {
// read the leafs/nodes and set the values
}

The remark by Resharper doesn't mean "you always should use
XContainer", but "in this particular method, you use only the
methods/properties of XElement's baseclass, XContainer. If you change
the parameter to be of type XContainer, you could use the same method
for other classes that derive from XContainer" (such as XDocument).

The same rule would fire if you have a Collection<T> parameter and you
use only the IEnumerable<T> functionality. When you change the
parameter to IEnumerable<T>, you could also use the same method to
process a List<T>.


Hans Kesting
 
M

Martin Honnen

K said:
Thanks for the reply. I'm taking it as a "no, don't change
to XContainer".

At least if you want to use the Parse or Load method. I don't know that
other thread, I simply posted based on the information in this thread.
 

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