-
获取XML节点的值(http服务使用xml传输数据,节点名称唯一)
/// <summary> /// 获取xml节点的值 /// </summary> /// <param name="xml">xml字符串</param> /// <param name="xmlnode">节点名</param> /// <returns>节点值</returns> public static string GetXmlNodeValue(string xml, string xmlnode) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); string val = XDocument.Load(new StringReader(xml)).Descendants(xmlnode).First().Value; return val; }
-
获取XML节点的内容(http服务使用xml传输数据,节点名称唯一)
/// <summary> /// 获取xml节点的内容 /// </summary> /// <param name="xml">xml字符串</param> /// <param name="xmlnode">节点名</param> /// <returns>节点值</returns> public static string GetXmlNodeNode(string xml, string xmlnode) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); object val = XDocument.Load(new StringReader(xml)).Descendants(xmlnode).First(); return val.ToString(); }
-
XML与DataSet互转(xml文件与DataTable格式一致)
private string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; XmlTextWriter writer = null; try { stream = new MemoryStream(); writer = new XmlTextWriter(stream, Encoding.Default); xmlDS.WriteXml(writer); int count = (int)stream.Length; byte[] arr = new byte[count]; stream.Seek(0, SeekOrigin.Begin); stream.Read(arr, 0, count); UTF8Encoding utf = new UTF8Encoding(); return utf.GetString(arr).Trim(); } catch { return String.Empty; } finally { if (writer != null) writer.Close(); } } private DataSet ConvertXMLToDataSet(string xmlData) { StringReader stream = null; XmlTextReader reader = null; try { DataSet xmlDS = new DataSet(); stream = new StringReader(xmlData); reader = new XmlTextReader(stream); xmlDS.ReadXml(reader); return xmlDS; } catch (Exception ex) { string strTest = ex.Message; return null; } finally { if (reader != null) reader.Close(); } }
- 获取节点的所有子节点,并用哈希表保存值
//获取节点的所有子节点,并用哈希表保存值 private Hashtable GetChildNodesHT(XmlDocument xmlDoc, string strNodeName) { Hashtable ht = new Hashtable(); XmlNodeList xnThis = xmlDoc.GetElementsByTagName(strNodeName); try { XmlNodeList childNodes = xnThis[0].ChildNodes; foreach (XmlNode xn in childNodes) { if (ht.ContainsKey(xn.Name) == false) { ht[xn.Name] = xn.InnerText; } } } catch { } return ht; }
jin 2011-2014