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(, SeekOrigin.Begin);
    stream.Read(arr, , 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[].ChildNodes;
foreach (XmlNode xn in childNodes)
{
if (ht.ContainsKey(xn.Name) == false)
{
ht[xn.Name] = xn.InnerText;
}
}
}
catch { }
return ht;
}

XmlHelper的更多相关文章

  1. 使用XmlHelper添加节点C#代码

    接着上一篇:http://keleyi.com/a/bjac/ttssua0f.htm在前篇文章中,给出了C# XML文件操作类XmlHelper的代码,以及使用该类的一个例子,即使用XmlHelpe ...

  2. (三)XmlHelper

    [转]http://blog.csdn.net/u011866450/article/details/50373222 using System.Xml; using System.Data; nam ...

  3. XMLHelper.cs

    http://yunpan.cn/Q7czcYTwE8qkc  提取码 545c using System; using System.Linq; using System.Xml.Linq; usi ...

  4. C#XmlHelper操作Xml文档的帮助类

    using System.Xml; using System.Data; namespace DotNet.Utilities { /// <summary> /// Xml的操作公共类 ...

  5. 转XMLHelper

    http://www.cnblogs.com/lixyvip/archive/2009/09/16/1567929.html using System; using System.Collection ...

  6. [XML] C# XmlHelper操作Xml文档的帮助类 (转载)

    点击下载 XmlHelper.rar 主要功能如下所示 /// <summary> /// 类说明:XmlHelper /// 编 码 人:苏飞 /// 联系方式:361983679 // ...

  7. C# XML文件操作类XmlHelper

    类的完整代码: using System;using System.Collections;using System.Xml; namespace Keleyi.Com.XmlDAL{public c ...

  8. XMLHelper 类

     这个XMLHelper类中包括了XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现,有兴趣的朋友,可以进来看看,所有代码都在WebForm和WinForm中调试通过. 这是下面 ...

  9. [No0000DE]C# XmlHelper XML类型操作 类封装

    using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...

  10. XMLHelper类 源码(XML文档帮助类,静态方法,实现对XML文档的创建,及节点和属性的增、删、改、查)

    以下是代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

随机推荐

  1. Protobuf3 + Netty4: 在socket上传输多种类型的protobuf数据

    Protobuf序列化的字节流数据是不能自描述的,当我们通过socket把数据发送到Client时,Client必须知道发送的是什么类型的数据,才能正确的反序列化它.这严重影响限制了C/S功能的实现, ...

  2. VS2013 带命令行参数的调试问题 解决方案

    int main(int argc,char* argv[]) argc是命令行总的参数个数,argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面跟的用户输入的参数 比如:  ...

  3. 阅读笔记 1 火球 UML大战需求分析

    伴随着七天国庆的结束,紧张的学习生活也开始了,首先声明,阅读笔记随着我不断地阅读进度会慢慢更新,而不是一次性的写完,所以会重复的编辑.对于我选的这本   <火球 UML大战需求分析>,首先 ...

  4. Apache配置文件讲解

        持续作用扩展自 HTTP/1.0 和 HTTP/1.1 的长连接特性.提供了长效的 HTTP 会话,用以在同 一个 TCP 连接中进行多次请求. 在某些情况下, 这样的方式会对包含大量图片的 ...

  5. 兼容IE7音乐播放器之jplayer的使用

    首先列出为何要写这篇随笔的原因: 1:兼容IE7 2:音乐播放器 3:任意控制播放器 1: 最近做的网站需要兼容IE7,在此之前已经写好了关于音乐播放的插件,火狐,IE8以上,以及谷歌浏览器等都可以随 ...

  6. ubuntu host备份 ubuntu无法解析主机

    /etc/hosts # Copyright (c) 2014-2016, racaljk.# https://github.com/racaljk/hosts# Last updated: 2016 ...

  7. 解决file_get_contents遇到中文文件名无法打开问题

    利用file_get_contents打开文件或采集远程服务器文件如果文名或url中碰到汉字中文那么会出现failed to open stream:Lnvalid argument in错误.   ...

  8. 微信小程序-媒体组件

    audio 音频. MediaError.code 示例代码: <!-- audio.wxml --> <audio poster="{{poster}}" na ...

  9. 微信小程序-基础内容组件

    icon 图标 示例: <view class="group"> <block wx:for="{{iconSize}}"> <i ...

  10. C++Primer 5th 练习 12.19

    这阵子真是太忙了, 连续做了四个课设. 当然这并不能作为好久没写博客的借口, 没写博客的主要原因只有一个: 懒. 最近又开始回顾C++的语法与特性(据说C++就是一门需要反复回顾的语言),以及学习C+ ...