Should i prefer XElement or XContainer?

  • Thread starter Thread starter K Viltersten
  • Start date Start date
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
}
 
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.
 
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.
 
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
 
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.
 
Back
Top