C#关于xml数据的解析
来源:互联网 发布:太平洋软件网站首页 编辑:程序博客网 时间:2024/05/16 05:04
源代码奉上....
using System;
using System.Net;using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections.Generic;
namespace Common
{
public class ReadXmlHelper
{
protected static string ReadXml(string xml)
{
StringBuilder builder = new StringBuilder();
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
while (reader.Read())
{
switch (reader.NodeType)
{ //注意:此处对于结点,在节点前加分号,将所有节点断开(使用根节点)
case XmlNodeType.EndElement: builder.Append(reader.Name + "|"); break;
case XmlNodeType.Text: builder.Append(reader.Value + "#"); break;
}
}
}
return builder.ToString();
}
/// <summary>
/// 对象集合写入字典(可以用多用户对象调试单用户)
/// </summary>
/// <param name="xml">XML文本</param>
/// <param name="type">处理的类型</param>
/// <returns>多用户字典</returns>
public static IDictionary<int, IDictionary<string, string>> GetDic(string xml, string type)
{
string content = ReadXml(xml).Replace("<span class=H>", null).Replace("</span>", null);
IDictionary<int, IDictionary<string, string>> dits = new Dictionary<int, IDictionary<string, string>>();
IDictionary<string, string> dit = new Dictionary<string, string>();
int i = 0;
string[] arrays = content.Split('|');
foreach (string item in arrays)
{
if (item == type)//最好是,根节点
{
if (dit.Count > 0)
{ i++; dits.Add(i, dit); dit = new Dictionary<string, string>(); }
}
if (!string.IsNullOrEmpty(item))
{
string[] param = item.Split('#');
if (!string.IsNullOrEmpty(param[0]))//注意:必须key值不为空,但value值可以为空
{
dit.Add(param[param.Length - 1], param[0]);
}
}
}
return dits;
}
}
}
个人博客:http://blog.amtemai.com
- C#关于xml数据的解析
- C#的XML解析
- 关于XML的解析
- 关于XML的解析
- 关于XML的解析
- xml数据的解析
- XML数据的解析
- xml数据的解析
- 关于swift里面xml数据的解析设置问题
- 关于xml和Json数据解析的一些总结
- 关于ios7 使用 GDataXml 解析 xml 数据的问题。
- c# 解析xml 的方法
- 关于xml的解析方法
- 关于XML解析的测试
- android关于xml的解析
- 关于xml解析的资料
- 网络数据的XML解析
- 网络数据的XML解析
- maven eclipse 集成
- WP7 http异步通信
- 精彩博客链接
- QQ在线,Q我吧
- HDU 4082 Hou Yi's secret
- C#关于xml数据的解析
- Oracle Flashback之Flashback table和flashback drop
- 幸福,很简单
- 命名空间小结
- 使用sqlcmd命令
- oracle更改用户密码过期,取消密码验证
- MTK平台源码学习笔记之-------宏定义的学习
- 比较两个数组中不同的值
- VIM 查找文本