| 12345678910111213141516171819202122232425 |
- using System;
- using System.ComponentModel;
- using System.Xml;
- namespace Quadarax.Foundation.Common.Xml.Extensions
- {
- public static class XmlReaderExt
- {
- public static T GetAttribute<T>(this XmlReader oNavigator, string sAttributeName, bool bMandatory, T oDefaultValue)
- {
- return (T) GetAttribute(oNavigator, typeof(T),sAttributeName,bMandatory,oDefaultValue);
- }
- public static object GetAttribute(this XmlReader oNavigator, Type oType, string sAttributeName, bool bMandatory, object oDefaultValue)
- {
- if (string.IsNullOrEmpty(oNavigator.GetAttribute(sAttributeName, "")))
- {
- if (bMandatory)
- throw new Exception(
- $"Missing mandatory attribute '{sAttributeName}' in element '{oNavigator.Name}'");
- return oDefaultValue;
- }
- return TypeDescriptor.GetConverter(oType).ConvertFromString(oNavigator.GetAttribute(sAttributeName, ""));
- }
- }
- }
|