Guest User

Untitled

a guest
Jun 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. private static bool GetNodeValue(this XmlDocument doc, string nodeName, out string value)
  2. {
  3. value = null;
  4.  
  5. if (doc == null)
  6. return false;
  7.  
  8. XmlNode node = doc.SelectSingleNode(nodeName);
  9.  
  10. if (node == null)
  11. return false;
  12.  
  13. value = node.InnerText;
  14.  
  15. return true;
  16. }
  17.  
  18. private static bool IsEmptyNodeValue(this XmlDocument doc, string nodeName, out string value)
  19. {
  20. return !doc.GetNodeValue(nodeName, out value) || string.IsNullOrEmpty(value);
  21. }
  22.  
  23. private static bool GetAttributeValue(this XmlNode node, string attributeName, out string value)
  24. {
  25. value = null;
  26.  
  27. if (node == null)
  28. return false;
  29.  
  30. XmlAttribute attribute = node.Attributes[attributeName];
  31.  
  32. if (attribute == null || !attribute.Specified)
  33. return false;
  34.  
  35. value = attribute.Value;
  36.  
  37. return true;
  38. }
  39.  
  40. private static bool IsEmptyAttributeValue(this XmlNode node, string attributeName, out string value)
  41. {
  42. return !node.GetAttributeValue(attributeName, out value) || string.IsNullOrEmpty(value);
  43. }
  44.  
  45. public static DateTime GetNode(this XmlDocument doc, string nodeName, DateTime defaultValue)
  46. {
  47. string nodeText;
  48.  
  49. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  50. return defaultValue;
  51.  
  52. DateTime nodeValue;
  53.  
  54. if (!DateTime.TryParseExact(nodeText, "F", IsoFormat, DateTimeStyles.None, out nodeValue))
  55. return defaultValue;
  56.  
  57. return nodeValue;
  58. }
  59.  
  60. public static DateTime GetAttribute(this XmlNode node, string attributeName, DateTime defaultValue)
  61. {
  62. string attributeText;
  63.  
  64. if (node.IsEmptyAttributeValue(attributeName, out attributeText))
  65. return defaultValue;
  66.  
  67. DateTime attributeValue;
  68.  
  69. if (!DateTime.TryParseExact(attributeText, "F", IsoFormat, DateTimeStyles.None, out attributeValue))
  70. return defaultValue;
  71.  
  72. return attributeValue;
  73. }
  74.  
  75. public static int GetNode(this XmlDocument doc, string nodeName, int defaultValue)
  76. {
  77. string nodeText;
  78.  
  79. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  80. return defaultValue;
  81.  
  82. int nodeValue;
  83.  
  84. if (!int.TryParse(nodeText, out nodeValue))
  85. return defaultValue;
  86.  
  87. return nodeValue;
  88. }
  89.  
  90. public static int GetAttribute(this XmlNode node, string attributeName, int defaultValue)
  91. {
  92. string attributeText;
  93.  
  94. if (node.IsEmptyAttributeValue(attributeName, out attributeText))
  95. return defaultValue;
  96.  
  97. int attributeValue;
  98.  
  99. if (!int.TryParse(attributeText, out attributeValue))
  100. return defaultValue;
  101.  
  102. return attributeValue;
  103. }
  104.  
  105. public static uint GetNode(this XmlDocument doc, string nodeName, uint defaultValue)
  106. {
  107. string nodeText;
  108.  
  109. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  110. return defaultValue;
  111.  
  112. uint nodeValue;
  113.  
  114. if (!uint.TryParse(nodeText, out nodeValue))
  115. return defaultValue;
  116.  
  117. return nodeValue;
  118. }
  119.  
  120. public static uint GetAttribute(this XmlNode node, string attributeName, uint defaultValue)
  121. {
  122. string attributeText;
  123.  
  124. if (node.IsEmptyAttributeValue(attributeName, out attributeText))
  125. return defaultValue;
  126.  
  127. uint attributeValue;
  128.  
  129. if (!uint.TryParse(attributeText, out attributeValue))
  130. return defaultValue;
  131.  
  132. return attributeValue;
  133. }
  134.  
  135. public static ushort GetNode(this XmlDocument doc, string nodeName, ushort defaultValue)
  136. {
  137. string nodeText;
  138.  
  139. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  140. return defaultValue;
  141.  
  142. ushort nodeValue;
  143.  
  144. if (!ushort.TryParse(nodeText, out nodeValue))
  145. return defaultValue;
  146.  
  147. return nodeValue;
  148. }
  149.  
  150. public static ushort GetAttribute(this XmlNode node, string attributeName, ushort defaultValue)
  151. {
  152. string attributeText;
  153.  
  154. if (node.IsEmptyAttributeValue(attributeName, out attributeText))
  155. return defaultValue;
  156.  
  157. ushort attributeValue;
  158.  
  159. if (!ushort.TryParse(attributeText, out attributeValue))
  160. return defaultValue;
  161.  
  162. return attributeValue;
  163. }
  164.  
  165. public static bool GetNode(this XmlDocument doc, string nodeName, bool defaultValue)
  166. {
  167. string nodeText;
  168.  
  169. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  170. return defaultValue;
  171.  
  172. bool nodeValue;
  173.  
  174. if (!bool.TryParse(nodeText, out nodeValue))
  175. return defaultValue;
  176.  
  177. return nodeValue;
  178. }
  179.  
  180. public static bool GetAttribute(this XmlNode node, string attributeName, bool defaultValue)
  181. {
  182. string attributeText;
  183.  
  184. if (node.IsEmptyAttributeValue(attributeName, out attributeText))
  185. return defaultValue;
  186.  
  187. bool attributeValue;
  188.  
  189. if (!bool.TryParse(attributeText, out attributeValue))
  190. return defaultValue;
  191.  
  192. return attributeValue;
  193. }
  194.  
  195. public static Guid GetNode(this XmlDocument doc, string nodeName, Guid defaultValue)
  196. {
  197. string nodeText;
  198.  
  199. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  200. return defaultValue;
  201.  
  202. Guid nodeValue;
  203.  
  204. if (!GuidTryParse(nodeText, out nodeValue))
  205. return defaultValue;
  206.  
  207. return nodeValue;
  208. }
  209.  
  210. public static Guid GetAttribute(this XmlNode node, string attributeName, Guid defaultValue)
  211. {
  212. string attributeText;
  213.  
  214. if (node.IsEmptyAttributeValue(attributeName, out attributeText))
  215. return defaultValue;
  216.  
  217. Guid attributeValue;
  218.  
  219. if (!GuidTryParse(attributeText, out attributeValue))
  220. return defaultValue;
  221.  
  222. return attributeValue;
  223. }
  224.  
  225. public static string GetNode(this XmlDocument doc, string nodeName, string defaultValue)
  226. {
  227. string nodeText;
  228.  
  229. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  230. return defaultValue;
  231.  
  232. return nodeText;
  233. }
  234.  
  235. public static string GetAttribute(this XmlNode node, string attributeName, string defaultValue)
  236. {
  237. string attributeText;
  238.  
  239. if (!GetAttributeValue(node, attributeName, out attributeText) || String.IsNullOrEmpty(attributeText))
  240. return defaultValue;
  241.  
  242. return attributeText;
  243. }
  244.  
  245. public static T GetNode<T>(this XmlDocument doc, string nodeName, T defaultValue)
  246. {
  247. return doc.GetNode(nodeName, defaultValue, CultureInfo.InvariantCulture);
  248. }
  249.  
  250. public static T GetAttribute<T>(this XmlNode node, string attributeName, T defaultValue)
  251. {
  252. return node.GetAttribute(attributeName, defaultValue, CultureInfo.InvariantCulture);
  253. }
  254.  
  255. public static T GetNode<T>(this XmlDocument doc, string nodeName, T defaultValue, IFormatProvider provider)
  256. {
  257. string nodeText;
  258.  
  259. if (doc.IsEmptyNodeValue(nodeName, out nodeText))
  260. return defaultValue;
  261.  
  262. return ChangeType<T>(nodeText, provider);
  263. }
  264.  
  265. public static T GetAttribute<T>(this XmlNode node, string attributeName, T defaultValue, IFormatProvider provider)
  266. {
  267. string attributeText;
  268.  
  269. if (!GetAttributeValue(node, attributeName, out attributeText) || String.IsNullOrEmpty(attributeText))
  270. return defaultValue;
  271.  
  272. return ChangeType<T>(attributeText, provider);
  273. }
  274.  
  275. public static string GetNodeXml(this XmlDocument doc, string nodeName, string defaultXml)
  276. {
  277. XmlNode node = doc.SelectSingleNode(nodeName);
  278.  
  279. if (node == null)
  280. return defaultXml;
  281.  
  282. string nodeXml = node.OuterXml;
  283.  
  284. if (string.IsNullOrEmpty(nodeXml))
  285. return defaultXml;
  286.  
  287. return nodeXml;
  288. }
Add Comment
Please, Sign In to add comment