• XmlHelper


    1. 获取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;
          }
    2. 获取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();
          }
    3. 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();
            }
          }
    4. 获取节点的所有子节点,并用哈希表保存值
    //获取节点的所有子节点,并用哈希表保存值 
        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
  • 相关阅读:
    回到顶部
    angularjs 复选框 单选框
    关于angularjs的ng-repeat指令
    JS字符串对象
    JS的控制语句与异常
    JS的运算符
    JS的引入方式和基础规范
    z-index及透明度opacity,利用overflow设置头像
    css的定位
    css的float属性及清除浮动
  • 原文地址:https://www.cnblogs.com/hongtian/p/3499504.html
Copyright © 2020-2023  润新知