I thought I would some extension methods I have been using for a little while now for Linq to Xml. When loading and retrieving the attribute values from XML nodes via Linq there is a bit of code to write to make sure the attribute exists and that the value is a valid data type. Rather than having a bunch of code to do this I simply wrote some extensions that operate on an XElement and are targeted at returning the value of an XAttribute.
Usage:
var element = XElement.Parse("<value x="50.5"/>");var value = element.GetDoubleValue("x");.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt{ background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt{ background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }
Anyway, here is the class for the extensions:
1: using System;
2: using System.Xml.Linq;
3:
4: namespace System.Xml.Linq
5: {
6: public static class XLinqEx
7: {
8: /// <summary>
9: /// Gets a short value for the specified attribute.
10: /// </summary>
11: /// <param name="element">The element to parse.</param>
12: /// <param name="attributeName">The XName of the attribute to parse.</param>
13: /// <returns>A short value representation of the attribute value.</returns>
14: public static short GetInt16Value(this XElement element, XName attributeName)
15: {
16: var attribute = element.Attribute(attributeName);
17: if (attribute == null)
18: return 0;
19:
20: short result = 0;
21: Int16.TryParse(attribute.Value, out result);
22: return result;
23: }
24:
25: /// <summary>
26: /// Gets an int value for the specified attribute.
27: /// </summary>
28: /// <param name="element">The element to parse.</param>
29: /// <param name="attributeName">The XName of the attribute to parse.</param>
30: /// <returns>An int value representation of the attribute value.</returns>
31: public static int GetInt32Value(this XElement element, XName attributeName)
32: {
33: var attribute = element.Attribute(attributeName);
34: if (attribute == null)
35: return 0;
36:
37: int result = 0;
38: Int32.TryParse(attribute.Value, out result);
39: return result;
40: }
41:
42: /// <summary>
43: /// Gets a long value for the specified attribute.
44: /// </summary>
45: /// <param name="element">The element to parse.</param>
46: /// <param name="attributeName">The XName of the attribute to parse.</param>
47: /// <returns>A long value representation of the attribute value.</returns>
48: public static long GetInt64Value(this XElement element, XName attributeName)
49: {
50: var attribute = element.Attribute(attributeName);
51: if (attribute == null)
52: return 0;
53:
54: long result = 0;
55: Int64.TryParse(attribute.Value, out result);
56: return result;
57: }
58:
59: /// <summary>
60: /// Gets a double value for the specified attribute.
61: /// </summary>
62: /// <param name="element">The element to parse.</param>
63: /// <param name="attributeName">The XName of the attribute to parse.</param>
64: /// <returns>A double value representation of the attribute value.</returns>
65: public static double GetDoubleValue(this XElement element, XName attributeName)
66: {
67: var attribute = element.Attribute(attributeName);
68: if (attribute == null)
69: return 0;
70:
71: double result = 0;
72: Double.TryParse(attribute.Value, out result);
73: return result;
74: }
75:
76: /// <summary>
77: /// Gets a bool value for the specified attribute.
78: /// </summary>
79: /// <param name="element">The element to parse.</param>
80: /// <param name="attributeName">The XName of the attribute to parse.</param>
81: /// <returns>A bool value representation of the attribute value.</returns>
82: public static bool GetBooleanValue(this XElement element, XName attributeName)
83: {
84: var attribute = element.Attribute(attributeName);
85: if (attribute == null)
86: return false;
87:
88: bool result = false;
89: Boolean.TryParse(attribute.Value, out result);
90: return result;
91: }
92:
93: /// <summary>
94: /// Gets a string value for the specified attribute.
95: /// </summary>
96: /// <param name="element">The element to parse.</param>
97: /// <param name="attributeName">The XName of the attribute to parse.</param>
98: /// <returns>A string value representation of the attribute value.</returns>
99: public static string GetStringValue(this XElement element, XName attributeName)
100: {
101: var attribute = element.Attribute(attributeName);
102: if (attribute == null)
103: return String.Empty;
104:
105: return attribute.Value;
106: }
107:
108: /// <summary>
109: /// Gets a byte array from the base 64 encoded value of the current element.
110: /// </summary>
111: /// <param name="element">The element to parse.</param>
112: /// <returns>A byte array of the base 64 encoded element value.</returns>
113: public static byte[] GetByteArray(this XElement element)
114: {
115: var value = element.Value;
116: if (!String.IsNullOrEmpty(value))
117: {
118: return Convert.FromBase64String(value);
119: }
120: return null;
121: }
122: }
123: }
1/19/2010