I am creating a parser for Web Services. I am in a road block.
I have an XML document which is
<AdvisorName>
<PersonNameTitle>String[]</PersonNameTitle>
<PersonGivenName>String[]</PersonGivenName>
<PersonFamilyName>String</PersonFamilyName>
<PersonNameSuffix>String[]</PersonNameSuffix>
<PersonRequestedName>String</PersonRequestedName>
</AdvisorName>
My code is
foreach (XElement childNodeprop in childNodesPropLst)
{
XElement childElement = childNodeprop.Element(prop.Name);
if (childElement != null)
{
// Error happens at next line:
prop.SetValue(obj, Convert.ChangeType(childElement.Value, prop.PropertyType),
null);
break;
}
}
As you had seen that the XML whose return type is an array it can't convert this.
Full Code is attached here with
foreach (var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.SetProperty))
{
if (!prop.Name.Equals("ExtensionData"))
{
if (prop.PropertyType.IsPrimitive())
{
var childNodesPropLst = doc.Descendants(propertyName);
foreach (XElement childNodeprop in childNodesPropLst)
{
XElement childElement = childNodeprop.Element(prop.Name);
if (childElement != null)
{
prop.SetValue(obj, Convert.ChangeType(childElement.Value, prop.PropertyType), null);
break;
}
}
}
}
}
I concur with Rufus that custom XML deserialization probably isn't necessary if using .NET's built-in XML serialization (Introducing XML Serialization), but if you must, you can try using TypeConverters like:
var type = //get type
TypeDescriptor.GetConverter(type).ConvertFrom(stringSerialization);