Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static bool GetNodeValue(this XmlDocument doc, string nodeName, out string value)
- {
- value = null;
- if (doc == null)
- return false;
- XmlNode node = doc.SelectSingleNode(nodeName);
- if (node == null)
- return false;
- value = node.InnerText;
- return true;
- }
- private static bool IsEmptyNodeValue(this XmlDocument doc, string nodeName, out string value)
- {
- return !doc.GetNodeValue(nodeName, out value) || string.IsNullOrEmpty(value);
- }
- private static bool GetAttributeValue(this XmlNode node, string attributeName, out string value)
- {
- value = null;
- if (node == null)
- return false;
- XmlAttribute attribute = node.Attributes[attributeName];
- if (attribute == null || !attribute.Specified)
- return false;
- value = attribute.Value;
- return true;
- }
- private static bool IsEmptyAttributeValue(this XmlNode node, string attributeName, out string value)
- {
- return !node.GetAttributeValue(attributeName, out value) || string.IsNullOrEmpty(value);
- }
- public static DateTime GetNode(this XmlDocument doc, string nodeName, DateTime defaultValue)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- DateTime nodeValue;
- if (!DateTime.TryParseExact(nodeText, "F", IsoFormat, DateTimeStyles.None, out nodeValue))
- return defaultValue;
- return nodeValue;
- }
- public static DateTime GetAttribute(this XmlNode node, string attributeName, DateTime defaultValue)
- {
- string attributeText;
- if (node.IsEmptyAttributeValue(attributeName, out attributeText))
- return defaultValue;
- DateTime attributeValue;
- if (!DateTime.TryParseExact(attributeText, "F", IsoFormat, DateTimeStyles.None, out attributeValue))
- return defaultValue;
- return attributeValue;
- }
- public static int GetNode(this XmlDocument doc, string nodeName, int defaultValue)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- int nodeValue;
- if (!int.TryParse(nodeText, out nodeValue))
- return defaultValue;
- return nodeValue;
- }
- public static int GetAttribute(this XmlNode node, string attributeName, int defaultValue)
- {
- string attributeText;
- if (node.IsEmptyAttributeValue(attributeName, out attributeText))
- return defaultValue;
- int attributeValue;
- if (!int.TryParse(attributeText, out attributeValue))
- return defaultValue;
- return attributeValue;
- }
- public static uint GetNode(this XmlDocument doc, string nodeName, uint defaultValue)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- uint nodeValue;
- if (!uint.TryParse(nodeText, out nodeValue))
- return defaultValue;
- return nodeValue;
- }
- public static uint GetAttribute(this XmlNode node, string attributeName, uint defaultValue)
- {
- string attributeText;
- if (node.IsEmptyAttributeValue(attributeName, out attributeText))
- return defaultValue;
- uint attributeValue;
- if (!uint.TryParse(attributeText, out attributeValue))
- return defaultValue;
- return attributeValue;
- }
- public static ushort GetNode(this XmlDocument doc, string nodeName, ushort defaultValue)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- ushort nodeValue;
- if (!ushort.TryParse(nodeText, out nodeValue))
- return defaultValue;
- return nodeValue;
- }
- public static ushort GetAttribute(this XmlNode node, string attributeName, ushort defaultValue)
- {
- string attributeText;
- if (node.IsEmptyAttributeValue(attributeName, out attributeText))
- return defaultValue;
- ushort attributeValue;
- if (!ushort.TryParse(attributeText, out attributeValue))
- return defaultValue;
- return attributeValue;
- }
- public static bool GetNode(this XmlDocument doc, string nodeName, bool defaultValue)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- bool nodeValue;
- if (!bool.TryParse(nodeText, out nodeValue))
- return defaultValue;
- return nodeValue;
- }
- public static bool GetAttribute(this XmlNode node, string attributeName, bool defaultValue)
- {
- string attributeText;
- if (node.IsEmptyAttributeValue(attributeName, out attributeText))
- return defaultValue;
- bool attributeValue;
- if (!bool.TryParse(attributeText, out attributeValue))
- return defaultValue;
- return attributeValue;
- }
- public static Guid GetNode(this XmlDocument doc, string nodeName, Guid defaultValue)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- Guid nodeValue;
- if (!GuidTryParse(nodeText, out nodeValue))
- return defaultValue;
- return nodeValue;
- }
- public static Guid GetAttribute(this XmlNode node, string attributeName, Guid defaultValue)
- {
- string attributeText;
- if (node.IsEmptyAttributeValue(attributeName, out attributeText))
- return defaultValue;
- Guid attributeValue;
- if (!GuidTryParse(attributeText, out attributeValue))
- return defaultValue;
- return attributeValue;
- }
- public static string GetNode(this XmlDocument doc, string nodeName, string defaultValue)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- return nodeText;
- }
- public static string GetAttribute(this XmlNode node, string attributeName, string defaultValue)
- {
- string attributeText;
- if (!GetAttributeValue(node, attributeName, out attributeText) || String.IsNullOrEmpty(attributeText))
- return defaultValue;
- return attributeText;
- }
- public static T GetNode<T>(this XmlDocument doc, string nodeName, T defaultValue)
- {
- return doc.GetNode(nodeName, defaultValue, CultureInfo.InvariantCulture);
- }
- public static T GetAttribute<T>(this XmlNode node, string attributeName, T defaultValue)
- {
- return node.GetAttribute(attributeName, defaultValue, CultureInfo.InvariantCulture);
- }
- public static T GetNode<T>(this XmlDocument doc, string nodeName, T defaultValue, IFormatProvider provider)
- {
- string nodeText;
- if (doc.IsEmptyNodeValue(nodeName, out nodeText))
- return defaultValue;
- return ChangeType<T>(nodeText, provider);
- }
- public static T GetAttribute<T>(this XmlNode node, string attributeName, T defaultValue, IFormatProvider provider)
- {
- string attributeText;
- if (!GetAttributeValue(node, attributeName, out attributeText) || String.IsNullOrEmpty(attributeText))
- return defaultValue;
- return ChangeType<T>(attributeText, provider);
- }
- public static string GetNodeXml(this XmlDocument doc, string nodeName, string defaultXml)
- {
- XmlNode node = doc.SelectSingleNode(nodeName);
- if (node == null)
- return defaultXml;
- string nodeXml = node.OuterXml;
- if (string.IsNullOrEmpty(nodeXml))
- return defaultXml;
- return nodeXml;
- }
Add Comment
Please, Sign In to add comment