質問

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);
}

他のヒント

I you change the XmlReader - why not change it to XDocument (which is far more simple than XmlDocument).

Regard your question, if I understand you, you just need the Value property

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top