zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    考研打卡_Day04
    考研打卡_Day03
    考研打卡-Day02
    吾日三省-归隐
    为什么要写博客?
    用C语言写一个Helloworld_实现第一步编译运行
    C语言中的结构体是怎么定义的_怎么使用?
    C语言的常用的数据类型有哪些_所占字节分别是多少
    Vim编辑器中查找关键词命令_查找与替换命令_多窗口命令
    Vim的基本操作命令与光标移动命令
  • 原文地址:https://www.cnblogs.com/hongtian/p/3499504.html
Copyright © 2011-2022 走看看