I'm trying to change my the XmlReader
I'm using to an XmlDocument
, but some methods don't seem to exist. I am looking for an equivalent for XmlReader.ReadContentAs
and XmlReader.ReadElementContentAs
.
Type myType;
if(myType == typeof(Boolean) || myType == typeof(Double))
{
object myvalue = _cReader.ReadElementContentAs(myType, null);
}
// should become:
if(myType == typeof(Boolean) || myType == typeof(Double))
{
object myvalue = xmlElement.ParseAnything(myType);
}
I'm not only doing this with Boolean
, but there could be multiple types that can be read in this manner. It could be that myType
is a Single
or Double
as well.
Simply parse XmlElement.InnerText property to specific type :
bool mybool = Boolean.Parse(myXmlElement.InnerText);
UPDATE :
you can use Convert.ChangeType() method to parse string from Xml to given Type
variable :
Type myType;
if(myType == typeof(Boolean) || myType == typeof(Double))
{
object myvalue = Convert.ChangeType(xmlElement.InnerText, myType);
}