using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Xml;
|
|
namespace Pcb.Common.Extension
|
{
|
public static class XmlExtension
|
{
|
public static string GetStringValueOrDefault(this XmlAttribute attr, string defaultValue = null)
|
{
|
if (attr != null)
|
{
|
return attr.Value.Trim();
|
}
|
|
return defaultValue;
|
}
|
|
public static decimal? GetNullableDecimalValueOrDefault(this XmlAttribute attr, decimal? defaultValue = null)
|
{
|
var value = attr.GetStringValueOrDefault();
|
if (value == null)
|
{
|
return defaultValue;
|
}
|
|
var d = 0m;
|
if (decimal.TryParse(value, out d))
|
{
|
return d;
|
}
|
else
|
{
|
return defaultValue;
|
}
|
}
|
|
public static int GetInt32ValueOrDefault(this XmlAttribute attr, int defaultValue = 0)
|
{
|
var value = attr.GetStringValueOrDefault();
|
if (value == null)
|
{
|
return defaultValue;
|
}
|
|
var d = 0;
|
if (int.TryParse(value, out d))
|
{
|
return d;
|
}
|
else
|
{
|
return defaultValue;
|
}
|
}
|
|
/// <summary>
|
/// 获取int32的值,如果不能转换则抛出异常
|
/// </summary>
|
/// <param name="attr"></param>
|
/// <returns></returns>
|
public static int GetInt32Value(this XmlAttribute attr)
|
{
|
return int.Parse(attr.Value);
|
}
|
|
public static bool IsAttributeNotNullAndEmpty(this XmlNode node, string attrName)
|
{
|
var attr = node.Attributes[attrName];
|
if (attr == null || string.IsNullOrWhiteSpace(attr.Value))
|
{
|
return false;
|
}
|
|
return true;
|
}
|
}
|
}
|