ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

Parse XML Physical Path to Class

Get XML from Physical Path and Parse into Class

Source

public static T DeserilizeXML<T>(this string physicalPath) where T : class
{
    try
    {
        if (!string.IsNullOrEmpty(physicalPath) && !string.IsNullOrWhiteSpace(physicalPath))
        {
            if (File.Exists(physicalPath))
            {
                try
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<T>));
                    using (TextReader textReader = new StreamReader(physicalPath))
                    {
                        return (T)xmlSerializer.Deserialize(textReader);
                    }
                }
                catch (Exception)
                {
                    return File.ReadAllText(physicalPath).ParseToClass<T>();
                }
            }
        }
    }
    catch (Exception)
    {

    }
    return null;
}

Example

none

Author: Keyur PANCHAL

Submitted on: 3 nov 2016

Language: C#

Type: XML, Class

Views: 1542