Mercurial > silverbladetech
comparison MetroWpf/MetroWpf.Framework/Extensions/XmlNodeExtensions.cs @ 15:060f02cd4591
Initial commit, pre airport work
author | stevenh7776 stevenhollidge@hotmail.com |
---|---|
date | Mon, 12 Mar 2012 23:05:21 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
14:741981715d94 | 15:060f02cd4591 |
---|---|
1 using System; | |
2 using System.Xml; | |
3 | |
4 namespace MetroWpf | |
5 { | |
6 /// <summary> | |
7 /// Extension methods for the XmlNode / XmlDocument classes and its sub classes | |
8 /// </summary> | |
9 public static class XmlNodeExtensions | |
10 { | |
11 #region · Extensions · | |
12 | |
13 /// <summary> | |
14 /// Appends a child to a XML node | |
15 /// </summary> | |
16 /// <param name="parentNode">The parent node.</param> | |
17 /// <param name="name">The name of the child node.</param> | |
18 /// <returns>The newly cerated XML node</returns> | |
19 public static XmlNode CreateChildNode(this XmlNode parentNode, string name) | |
20 { | |
21 XmlDocument document = (parentNode is XmlDocument ? (XmlDocument)parentNode : parentNode.OwnerDocument); | |
22 XmlNode node = document.CreateElement(name); | |
23 | |
24 parentNode.AppendChild(node); | |
25 | |
26 return node; | |
27 } | |
28 | |
29 /// <summary> | |
30 /// Appends a child to a XML node | |
31 /// </summary> | |
32 /// <param name="parentNode">The parent node.</param> | |
33 /// <param name="name">The name of the child node.</param> | |
34 /// <param name="namespaceUri">The node namespace.</param> | |
35 /// <returns>The newly cerated XML node</returns> | |
36 public static XmlNode CreateChildNode(this XmlNode parentNode, string name, string namespaceUri) | |
37 { | |
38 XmlDocument document = (parentNode is XmlDocument ? (XmlDocument)parentNode : parentNode.OwnerDocument); | |
39 XmlNode node = document.CreateElement(name, namespaceUri); | |
40 | |
41 parentNode.AppendChild(node); | |
42 | |
43 return node; | |
44 } | |
45 | |
46 /// <summary> | |
47 /// Appends a CData section to a XML node | |
48 /// </summary> | |
49 /// <param name="parentNode">The parent node.</param> | |
50 /// <returns>The created CData Section</returns> | |
51 public static XmlCDataSection CreateCDataSection(this XmlNode parentNode) | |
52 { | |
53 return parentNode.CreateCDataSection(string.Empty); | |
54 } | |
55 | |
56 /// <summary> | |
57 /// Appends a CData section to a XML node and prefills the provided data | |
58 /// </summary> | |
59 /// <param name="parentNode">The parent node.</param> | |
60 /// <param name="data">The CData section value.</param> | |
61 /// <returns>The created CData Section</returns> | |
62 public static XmlCDataSection CreateCDataSection(this XmlNode parentNode, string data) | |
63 { | |
64 XmlDocument document = (parentNode is XmlDocument ? (XmlDocument)parentNode : parentNode.OwnerDocument); | |
65 XmlCDataSection node = document.CreateCDataSection(data); | |
66 | |
67 parentNode.AppendChild(node); | |
68 | |
69 return node; | |
70 } | |
71 | |
72 /// <summary> | |
73 /// Returns the value of a nested CData section. | |
74 /// </summary> | |
75 /// <param name="parentNode">The parent node.</param> | |
76 /// <returns>The CData section content</returns> | |
77 public static string GetCDataSection(this XmlNode parentNode) | |
78 { | |
79 foreach (var node in parentNode.ChildNodes) | |
80 { | |
81 if (node is XmlCDataSection) | |
82 { | |
83 return ((XmlCDataSection)node).Value; | |
84 } | |
85 } | |
86 | |
87 return null; | |
88 } | |
89 | |
90 /// <summary> | |
91 /// Gets an attribute value | |
92 /// </summary> | |
93 /// <param name="node">The node.</param> | |
94 /// <param name="attributeName">The Name of the attribute.</param> | |
95 /// <returns>The attribute value</returns> | |
96 public static string GetAttribute(this XmlNode node, string attributeName) | |
97 { | |
98 return GetAttribute(node, attributeName, null); | |
99 } | |
100 | |
101 /// <summary> | |
102 /// Gets an attribute value | |
103 /// </summary> | |
104 /// <param name="node">The node.</param> | |
105 /// <param name="attributeName">The Name of the attribute.</param> | |
106 /// <param name="defaultValue">The default value to be returned if no matching attribute exists.</param> | |
107 /// <returns>The attribute value</returns> | |
108 public static string GetAttribute(this XmlNode node, string attributeName, string defaultValue) | |
109 { | |
110 XmlAttribute attribute = node.Attributes[attributeName]; | |
111 | |
112 return (attribute != null ? attribute.InnerText : defaultValue); | |
113 } | |
114 | |
115 /// <summary> | |
116 /// Gets an attribute value converted to the specified data type | |
117 /// </summary> | |
118 /// <typeparam name="T">The desired return data type</typeparam> | |
119 /// <param name="node">The node.</param> | |
120 /// <param name="attributeName">The Name of the attribute.</param> | |
121 /// <returns>The attribute value</returns> | |
122 public static T GetAttribute<T>(this XmlNode node, string attributeName) | |
123 { | |
124 return GetAttribute<T>(node, attributeName, default(T)); | |
125 } | |
126 | |
127 /// <summary> | |
128 /// Gets an attribute value converted to the specified data type | |
129 /// </summary> | |
130 /// <typeparam name="T">The desired return data type</typeparam> | |
131 /// <param name="node">The node.</param> | |
132 /// <param name="attributeName">The Name of the attribute.</param> | |
133 /// <param name="defaultValue">The default value to be returned if no matching attribute exists.</param> | |
134 /// <returns>The attribute value</returns> | |
135 public static T GetAttribute<T>(this XmlNode node, string attributeName, T defaultValue) | |
136 { | |
137 var value = GetAttribute(node, attributeName); | |
138 | |
139 return (value.IsNotEmpty() ? value.ConvertTo<T>(defaultValue) : defaultValue); | |
140 } | |
141 | |
142 /// <summary> | |
143 /// Creates or updates an attribute with the passed value. | |
144 /// </summary> | |
145 /// <param name="node">The node.</param> | |
146 /// <param name="name">The name.</param> | |
147 /// <param name="value">The value.</param> | |
148 public static void SetAttribute(this XmlNode node, string name, object value) | |
149 { | |
150 SetAttribute(node, name, value != null ? value.ToString() : null); | |
151 } | |
152 | |
153 /// <summary> | |
154 /// Creates or updates an attribute with the passed value. | |
155 /// </summary> | |
156 /// <param name="node">The node.</param> | |
157 /// <param name="name">The name.</param> | |
158 /// <param name="value">The value.</param> | |
159 public static void SetAttribute(this XmlNode node, string name, string value) | |
160 { | |
161 if (node != null) | |
162 { | |
163 var attribute = node.Attributes[name, node.NamespaceURI]; | |
164 | |
165 if (attribute == null) | |
166 { | |
167 attribute = node.OwnerDocument.CreateAttribute(name, node.OwnerDocument.NamespaceURI); | |
168 node.Attributes.Append(attribute); | |
169 } | |
170 | |
171 attribute.InnerText = value; | |
172 } | |
173 } | |
174 | |
175 #endregion | |
176 } | |
177 } |