您目前尚未登陆,请选择【登陆】或【注册
首页->全站代码->IFNuke1.1.0版源码>>Core/XmlUtils.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:IFNuke1.1.0版源码
当前文件:文件类型 IFnuke110/Core/XmlUtils.cs打开代码结构图
普通视图
		            
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.IO; 5using System.Xml; 6using System.Xml.XPath; 7using System.Xml.Serialization; 8 9 10namespace IFNuke 11{ 12 13 /// ----------------------------------------------------------------------------- 14 /// <summary> 15 /// The XmlUtils class provides Shared/Static methods for manipulating xml files 16 /// </summary> 17 /// <remarks> 18 /// </remarks> 19 /// <history> 20 /// [cnurse] 11/08/2004 created 21 /// </history> 22 /// ----------------------------------------------------------------------------- 23 public class XmlUtils 24 { 25 26 27 public static void AppendElement(ref XmlDocument objDoc, XmlNode objNode, string attName, string attValue, bool includeIfEmpty) 28 { 29 AppendElement(objDoc, objNode, attName, attValue, includeIfEmpty, false); 30 } 31 32 public static void AppendElement(XmlDocument objDoc, XmlNode objNode, string attName, string attValue, bool includeIfEmpty, bool CDATA) 33 { 34 if (attValue == "" && !includeIfEmpty) 35 { 36 return; 37 } 38 if (CDATA) 39 { 40 objNode.AppendChild(CreateCDataElement(objDoc, attName, attValue)); 41 } 42 else 43 { 44 objNode.AppendChild(CreateElement(objDoc, attName, attValue)); 45 } 46 } 47 48 public static XmlAttribute CreateAttribute(XmlDocument objDoc, string attName, string attValue) 49 { 50 XmlAttribute attribute = objDoc.CreateAttribute(attName); 51 attribute.Value = attValue; 52 return attribute; 53 } 54 55 public static void CreateAttribute(XmlDocument objDoc, XmlNode objNode, string attName, string attValue) 56 { 57 XmlAttribute attribute = objDoc.CreateAttribute(attName); 58 attribute.Value = attValue; 59 objNode.Attributes.Append(attribute); 60 } 61 62 public static XmlElement CreateElement(XmlDocument objDoc, string NodeName) 63 { 64 return objDoc.CreateElement(NodeName); 65 } 66 67 public static XmlElement CreateElement(XmlDocument objDoc, string NodeName, string NodeValue) 68 { 69 XmlElement element = objDoc.CreateElement(NodeName); 70 element.InnerText = NodeValue; 71 return element; 72 } 73 74 public static XmlElement CreateCDataElement(XmlDocument objDoc, string NodeName, string NodeValue) 75 { 76 XmlElement element = objDoc.CreateElement(NodeName); 77 element.AppendChild(objDoc.CreateCDataSection(NodeValue)); 78 return element; 79 } 80 81 public static object Deserialize(string xmlObject, System.Type type) 82 { 83 XmlSerializer ser = new XmlSerializer(type); 84 StringReader sr = new StringReader(xmlObject); 85 object obj = ser.Deserialize(sr); 86 sr.Close(); 87 return obj; 88 } 89 90 public static object Deserialize(Stream objStream, System.Type type) 91 { 92 return new Object(); 93 //object obj = Activator.CreateInstance(type); 94 95 //Dictionary<int, DotNetNuke.Entities.Tabs.TabInfo> tabDic = obj as Dictionary<int, DotNetNuke.Entities.Tabs.TabInfo>; 96 //if (tabDic != null) 97 //{ 98 // obj = DeSerializeDictionary<TabInfo>(objStream, "dictionary"); 99 // return obj; 100 //} 101 //Dictionary<int, DotNetNuke.Entities.Modules.ModuleInfo> moduleDic = obj as Dictionary<int, DotNetNuke.Entities.Modules.ModuleInfo>; 102 //if (moduleDic != null) 103 //{ 104 // obj = DeSerializeDictionary<DotNetNuke.Entities.Modules.ModuleInfo>(objStream, "dictionary"); 105 // return obj; 106 //} 107 //Dictionary<int, DotNetNuke.security.Permissions.TabPermissionCollection> tabPermDic = obj as Dictionary<int, DotNetNuke.security.Permissions.TabPermissionCollection>; 108 //if (tabPermDic != null) 109 //{ 110 // obj = DeSerializeDictionary<Permissions.TabPermissionCollection>(objStream, "dictionary"); 111 // return obj; 112 //} 113 //Dictionary<int, DotNetNuke.security.Permissions.ModulePermissionCollection> modPermDic = obj as Dictionary<int, DotNetNuke.security.Permissions.ModulePermissionCollection>; 114 //if (modPermDic != null) 115 //{ 116 // obj = DeSerializeDictionary<Permissions.ModulePermissionCollection>(objStream, "dictionary"); 117 // return obj; 118 //} 119 120 //XmlSerializer serializer = new XmlSerializer(type); 121 //TextReader tr = new StreamReader(objStream); 122 //obj = serializer.Deserialize(tr); 123 //tr.Close(); 124 125 //return obj; 126 } 127 128 public static Dictionary<int, TValue> DeSerializeDictionary<TValue>(Stream objStream, string rootname) 129 { 130 Dictionary<int, TValue> objDictionary = null; 131 132 XmlDocument xmlDoc = new XmlDocument(); 133 xmlDoc.Load(objStream); 134 135 objDictionary = new Dictionary<int, TValue>(); 136 137 foreach (XmlElement xmlItem in xmlDoc.SelectNodes(rootname + "/item")) 138 { 139 int key = Convert.ToInt32(xmlItem.GetAttribute("key")); 140 string typeName = xmlItem.GetAttribute("type"); 141 142 TValue objValue = Activator.CreateInstance<TValue>(); 143 144 //Create the XmlSerializer 145 XmlSerializer xser = new XmlSerializer(objValue.GetType()); 146 147 //A reader is needed to read the XML document. 148 XmlTextReader reader = new XmlTextReader(new StringReader(xmlItem.InnerXml)); 149 150 // Use the Deserialize method to restore the object's state, and store it 151 // in the Hashtable 152 objDictionary.Add(key, ((TValue) (xser.Deserialize(reader)))); 153 } 154 155 return objDictionary; 156 } 157 158 public static Hashtable DeSerializeHashtable(string xmlSource, string rootname) 159 { 160 Hashtable objHashTable; 161 if (xmlSource != "") 162 { 163 objHashTable = new Hashtable(); 164 165 XmlDocument xmlDoc = new XmlDocument(); 166 xmlDoc.LoadXml(xmlSource); 167 168 foreach (XmlElement xmlItem in xmlDoc.SelectNodes(rootname + "/item")) 169 { 170 string key = xmlItem.GetAttribute("key"); 171 string typeName = xmlItem.GetAttribute("type"); 172 173 //Create the XmlSerializer 174 XmlSerializer xser = new XmlSerializer(Type.GetType(typeName)); 175 176 //A reader is needed to read the XML document. 177 XmlTextReader reader = new XmlTextReader(new StringReader(xmlItem.InnerXml)); 178 179 // Use the Deserialize method to restore the object's state, and store it 180 // in the Hashtable 181 objHashTable.Add(key, xser.Deserialize(reader)); 182 } 183 } 184 else 185 { 186 objHashTable = new Hashtable(); 187 } 188 return objHashTable; 189 } 190 191 /// ----------------------------------------------------------------------------- 192 /// <summary> 193 /// Gets the value of a node 194 /// </summary> 195 /// <param name="nav">Parent XPathNavigator</param> 196 /// <param name="path">The Xpath expression to the value</param> 197 /// <returns></returns> 198 /// <history> 199 /// [cnurse] 11/08/2004 moved from PortalController and made Public Shared 200 /// </history> 201 /// ----------------------------------------------------------------------------- 202 public static string GetNodeValue(XPathNavigator nav, string path) 203 { 204 string strValue = string.Empty; 205 206 XPathNavigator elementNav = nav.SelectSingleNode(path); 207 if (elementNav != null) 208 { 209 strValue = elementNav.Value; 210 } 211 212 return strValue; 213 214 } 215 216 /// ----------------------------------------------------------------------------- 217 /// <summary> 218 /// Gets the value of node 219 /// </summary> 220 /// <param name="objNode">Parent node</param> 221 /// <param name="NodeName">Child node to look for</param> 222 /// <param name="DefaultValue">Default value to return</param> 223 /// <returns></returns> 224 /// <remarks> 225 /// If the node does not exist or it causes any error the default value will be returned. 226 /// </remarks> 227 /// <history> 228 /// [VMasanas] 09/09/2004 Created 229 /// [cnurse] 11/08/2004 moved from PortalController and made Public Shared 230 /// </history> 231 /// ----------------------------------------------------------------------------- 232 public static string GetNodeValue(XmlNode objNode, string NodeName, string DefaultValue) 233 { 234 string strValue = DefaultValue; 235 236 try 237 { 238 strValue = objNode[NodeName].InnerText; 239 240 if (strValue == "" && DefaultValue != "") 241 { 242 strValue = DefaultValue; 243 } 244 } 245 catch 246 { 247 // node does not exist - legacy issue 248 } 249 250 return strValue; 251 252 } 253 254 /// ----------------------------------------------------------------------------- 255 /// <summary> 256 /// Gets the value of node 257 /// </summary> 258 /// <param name="objNode">Parent node</param> 259 /// <param name="NodeName">Child node to look for</param> 260 /// <param name="DefaultValue">Default value to return</param> 261 /// <returns></returns> 262 /// <remarks> 263 /// If the node does not exist or it causes any error the default value will be returned. 264 /// </remarks> 265 /// <history> 266 /// [VMasanas] 09/09/2004 Added new method to return converted values 267 /// [cnurse] 11/08/2004 moved from PortalController and made Public Shared 268 /// </history> 269 /// ----------------------------------------------------------------------------- 270 public static bool GetNodeValueBoolean(XmlNode objNode, string NodeName, bool DefaultValue) 271 { 272 bool bValue = DefaultValue; 273 274 try 275 { 276 bValue = Convert.ToBoolean(objNode[NodeName].InnerText); 277 } 278 catch 279 { 280 // node does not exist / data conversion error - legacy issue: use default value 281 } 282 283 return bValue; 284 285 } 286 287 /// ----------------------------------------------------------------------------- 288 /// <summary> 289 /// Gets the value of node 290 /// </summary> 291 /// <param name="objNode">Parent node</param> 292 /// <param name="NodeName">Child node to look for</param> 293 /// <param name="DefaultValue">Default value to return</param> 294 /// <returns></returns> 295 /// <remarks> 296 /// If the node does not exist or it causes any error the default value will be returned. 297 /// </remarks> 298 /// <history> 299 /// [VMasanas] 09/09/2004 Added new method to return converted values 300 /// [cnurse] 11/08/2004 moved from PortalController and made Public Shared 301 /// </history> 302 /// ----------------------------------------------------------------------------- 303 public static DateTime GetNodeValueDate(XmlNode objNode, string NodeName, DateTime DefaultValue) 304 { 305 DateTime dateValue = DefaultValue; 306 307 try 308 { 309 if (objNode[NodeName].InnerText != "") 310 { 311 312 dateValue = Convert.ToDateTime(objNode[NodeName].InnerText); 313 //if (dateValue.Date.Equals(Null.NullDate.Date)) 314 //{ 315 // dateValue = Null.NullDate; 316 //} 317 } 318 } 319 catch 320 { 321 // node does not exist / data conversion error - legacy issue: use default value 322 } 323