I have a database-column that contains some XML-data as a string. Hence I do not know the actual type that is represented by this xml I want to read the root-tag of this XML and than deserialize the complete content with this type. Because the embedded XML may be quite large and the table contains some thousand of these objects I need a fast solution for this. My first approach was on simply extracting the root-tag using some string-magic (probably using Regex), get the type by calling Type.GetType
and then create the serializer for this type. But than I had a look on XMLReader
which also supports a ValueType
-property.
using (XmlReader reader = XmlReader.Create(new StringReader(myXmlAsString)))
{
reader.MoveToContent(); // get the root-element
Type type = reader.ValueType;
XmlSerializer ser = new XmlSerializer(type);
return ser.Deserialize(reader);
}
The problem I have to face is that reader.ValueType
always returns string-type rather then the type represented by the root-tag.
Finally: which of the two solutions would be faster? Bottleneck on first one is supposed to be the regex-engine to get the tapeName, on second approach it might be reader-operations.