『代码』扩展一哈 XmlNode 的取值
1 #region 扩 展 XmlNode 取 值 2 3 internal static string ReadInnerString(this XmlNode node, string keyNode = @"", string defaultValue = @"") 4 { 5 if (node == null) return defaultValue; 6 if (string.IsNullOrWhiteSpace(keyNode)) 7 { 8 string innerXml = node.InnerXml ?? defaultValue; 9 if (innerXml != null) innerXml = innerXml.Trim(); 10 return XmlDecode(innerXml); 11 } 12 else 13 { 14 XmlNode childNode = node.SelectSingleNode(keyNode); //从子节点中 获取 String 15 return ReadInnerString(childNode, string.Empty, defaultValue); 16 } 17 } 18 internal static int ReadInnerInt32(this XmlNode node, string keyNode = @"", int defaultValue = 0) 19 { 20 if (node == null) return defaultValue; 21 if (string.IsNullOrWhiteSpace(keyNode)) 22 { 23 string innerXml = (node.InnerXml ?? string.Empty).Trim(); 24 if (string.IsNullOrEmpty(innerXml)) 25 return defaultValue; 26 27 int result; 28 int.TryParse(innerXml, out result); 29 return result; 30 } 31 else 32 { 33 XmlNode childNode = node.SelectSingleNode(keyNode); //从子节点中 获取 Int32 34 return ReadInnerInt32(childNode, string.Empty, defaultValue); 35 } 36 } 37 internal static long ReadInnerInt64(this XmlNode node, string keyNode = @"", long defaultValue = 0) 38 { 39 if (node == null) return defaultValue; 40 if (string.IsNullOrWhiteSpace(keyNode)) 41 { 42 string innerXml = (node.InnerXml ?? string.Empty).Trim(); 43 if (string.IsNullOrEmpty(innerXml)) 44 return defaultValue; 45 46 long result; 47 long.TryParse(innerXml, out result); 48 return result; 49 } 50 else 51 { 52 XmlNode childNode = node.SelectSingleNode(keyNode); //从子节点中 获取 Int64 53 return ReadInnerInt64(childNode, string.Empty, defaultValue); 54 } 55 } 56 internal static DateTime ReadInnerDateTime(this XmlNode node, string keyNode = @"") 57 { 58 return ReadInnerDateTime(node, new DateTime(1900, 1, 1), keyNode); 59 } 60 internal static DateTime ReadInnerDateTime(this XmlNode node, DateTime defaultValue, string keyNode = @"") 61 { 62 if (node == null) return defaultValue; 63 if (string.IsNullOrWhiteSpace(keyNode)) 64 { 65 string innerXml = (node.InnerXml ?? string.Empty).Trim(); 66 if (string.IsNullOrEmpty(innerXml)) 67 return defaultValue; 68 69 DateTime result; 70 DateTime.TryParse(innerXml, out result); 71 if (result.Year < 1900) result = defaultValue; 72 return result; 73 } 74 else 75 { 76 XmlNode childNode = node.SelectSingleNode(keyNode); //从子节点中 获取 DateTime 77 return ReadInnerDateTime(childNode, defaultValue, string.Empty); 78 } 79 } 80 internal static bool ReadInnerBoolean(this XmlNode node, string keyNode = @"", bool defaultValue = false) 81 { 82 if (node == null) return defaultValue; 83 if (string.IsNullOrWhiteSpace(keyNode)) 84 { 85 string innerXml = (node.InnerXml ?? string.Empty).Trim().ToLower(); 86 if (string.IsNullOrEmpty(innerXml)) 87 return defaultValue; 88 89 bool isTrue = string.Equals(innerXml, "true") || string.Equals(innerXml, "1") || string.Equals(innerXml, "t"); 90 if (isTrue) 91 return true; 92 else 93 { 94 bool isFalse = string.Equals(innerXml, "false") || string.Equals(innerXml, "0") || string.Equals(innerXml, "f"); 95 if (isFalse) return false; 96 } 97 98 return defaultValue; 99 } 100 else 101 { 102 XmlNode childNode = node.SelectSingleNode(keyNode); //从子节点中 获取 Boolean 103 return ReadInnerBoolean(childNode, string.Empty, defaultValue); 104 } 105 } 106 107 internal static string ReadAttrString(this XmlNode node, string attrName, string defaultValue = @"") 108 { 109 if (node == null) return defaultValue; 110 if (string.IsNullOrWhiteSpace(attrName)) 111 { 112 return ReadInnerString(node, attrName, defaultValue); 113 } 114 else 115 { 116 XmlAttribute attr = node.Attributes == null ? null : node.Attributes[attrName]; 117 string attrValue = attr != null ? attr.Value : defaultValue; 118 if (attrValue != null) attrValue = attrValue.Trim(); 119 return XmlDecode(attrValue); 120 } 121 } 122 internal static int ReadAttrInt32(this XmlNode node, string attrName, int defaultValue = 0) 123 { 124 if (node == null) return defaultValue; 125 if (string.IsNullOrWhiteSpace(attrName)) 126 { 127 return ReadInnerInt32(node, attrName, defaultValue); 128 } 129 else 130 { 131 XmlAttribute attr = node.Attributes == null ? null : node.Attributes[attrName]; 132 string attrValue = attr != null ? attr.Value : null; 133 int result = defaultValue; 134 if (!string.IsNullOrWhiteSpace(attrValue)) 135 { 136 attrValue = attrValue.Trim(); 137 int.TryParse(attrValue, out result); 138 } 139 return result; 140 } 141 } 142 internal static long ReadAttrInt64(this XmlNode node, string attrName, long defaultValue = 0) 143 { 144 if (node == null) return defaultValue; 145 if (string.IsNullOrWhiteSpace(attrName)) 146 { 147 return ReadInnerInt64(node, attrName, defaultValue); 148 } 149 else 150 { 151 XmlAttribute attr = node.Attributes == null ? null : node.Attributes[attrName]; 152 string attrValue = attr != null ? attr.Value : null; 153 long result = defaultValue; 154 if (!string.IsNullOrWhiteSpace(attrValue)) 155 { 156 attrValue = attrValue.Trim(); 157 long.TryParse(attrValue, out result); 158 } 159 return result; 160 } 161 } 162 internal static DateTime ReadAttrDateTime(this XmlNode node, string attrName) 163 { 164 return ReadAttrDateTime(node, attrName, new DateTime(1900, 1, 1)); 165 } 166 internal static DateTime ReadAttrDateTime(this XmlNode node, string attrName, DateTime defaultValue) 167 { 168 if (node == null) return defaultValue; 169 if (string.IsNullOrWhiteSpace(attrName)) 170 { 171 return ReadInnerDateTime(node, defaultValue, attrName); 172 } 173 else 174 { 175 XmlAttribute attr = node.Attributes == null ? null : node.Attributes[attrName]; 176 string attrValue = attr != null ? attr.Value : null; 177 DateTime result = defaultValue; 178 if (!string.IsNullOrWhiteSpace(attrValue)) 179 { 180 attrValue = attrValue.Trim(); 181 DateTime.TryParse(attrValue, out result); 182 } 183 return result; 184 } 185 } 186 internal static bool ReadAttrBoolean(this XmlNode node, string attrName, bool defaultValue = false) 187 { 188 if (node == null) return defaultValue; 189 if (string.IsNullOrWhiteSpace(attrName)) 190 { 191 return ReadInnerBoolean(node, attrName, defaultValue); 192 } 193 else 194 { 195 XmlAttribute attr = node.Attributes == null ? null : node.Attributes[attrName]; 196 string attrValue = attr != null ? attr.Value : null; 197 if (!string.IsNullOrWhiteSpace(attrValue)) 198 { 199 attrValue = attrValue.Trim(); 200 bool isTrue = string.Equals(attrValue, "true") || string.Equals(attrValue, "1") || string.Equals(attrValue, "t"); 201 if (isTrue) 202 return true; 203 else 204 { 205 bool isFalse = string.Equals(attrValue, "false") || string.Equals(attrValue, "0") || string.Equals(attrValue, "f"); 206 if (isFalse) return false; 207 } 208 } 209 return defaultValue; 210 } 211 } 212 213 public static string XmlEncode(string xmlValue) 214 { 215 xmlValue = xmlValue.Replace("&", "&"); 216 xmlValue = xmlValue.Replace("<", "<"); 217 xmlValue = xmlValue.Replace(">", ">"); 218 xmlValue = xmlValue.Replace("\'", "'"); 219 xmlValue = xmlValue.Replace("\"", """); 220 return xmlValue; 221 } 222 public static string XmlDecode(string xmlValue) 223 { 224 xmlValue = xmlValue.Replace("&", "&"); 225 xmlValue = xmlValue.Replace("<", "<"); 226 xmlValue = xmlValue.Replace(">", ">"); 227 xmlValue = xmlValue.Replace("'", "\'"); 228 xmlValue = xmlValue.Replace(""", "\""); 229 return xmlValue; 230 } 231 232 #endregion
分类:
练手片段调错
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述