数据字典Dictionary存放键值对
1. 方法思路:
使用数据字典【Dictionary<string, string>】,声明一个list集合,将“XML子节点名称”、“节点值”以键【节点名称】值【节点值】对的形式存入此集合,然后将此集合作为参数传入封装的公共方法中即可;
2. 公共方法:
public static string AssembleXML(Dictionary<string,string> list)
{
try
{
string strXML = "";
foreach (KeyValuePair<string, string> de in list)
{
strXML += "<" + de.Key + ">" + de.Value + "</" + de.Key + ">";
}
return strXML;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "组装XML时出现错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
return "";
}
}
3. 对公共方法的调用:
static void Main(string[] args)
{
string strXML交换 = "";
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("姓名","张三"); //xml节点、节点值
list.Add("年龄", "");
string strResult = AssembleXML(list);
if (strResult=="")
{
MessageBox.Show("组装xml出现错误!");
}
else
{
strXML交换 = @"<?xml version='1.0' encoding='GBK'?><ROWSET><ROW>" + strResult + "</ROW></ROWSET>";
}
Console.WriteLine(strXML交换);
Console.ReadKey();
}
4. 整体的完整代码块:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace TestPublicXML
{
class Program
{
static void Main(string[] args)
{
string strXML交换 = "";
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("姓名","张三"); //xml节点、节点值
list.Add("年龄", "");
string strResult = AssembleXML(list);
if (strResult=="")
{
MessageBox.Show("组装xml出现错误!");
}
else
{
strXML交换 = @"<?xml version='1.0' encoding='GBK'?><ROWSET><ROW>" + strResult + "</ROW></ROWSET>";
}
Console.WriteLine(strXML交换);
Console.ReadKey();
} public static string AssembleXML(Dictionary<string,string> list)
{
try
{
string strXML = "";
foreach (KeyValuePair<string, string> de in list)
{
strXML += "<" + de.Key + ">" + de.Value + "</" + de.Key + ">";
}
return strXML;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "组装XML时出现错误:", MessageBoxButtons.OK, MessageBoxIcon.Error);
return "";
}
}
}
}
数据字典Dictionary存放键值对的更多相关文章
- 04.Dictionary字典键值对集合
Dictionary字典键值对集合和Hashtable键值对集合的功能非常类似, 只是在声明的时候,必须为其制定值的类型. 示例代码: namespace _11.Dictionary字典集合的学习 ...
- Dictionary读取键值的快捷方法
对泛型集合Dictionary<T,T> 进行读取键值是经常的操作,一般情况下,都是通过keys 和values进行键值的读取操作: eg: foreach (var item in di ...
- C语言定义从URL中获取键值的接口
环境:centos7下,对客户端http请求进行解析,来获取有效键值(包括汉字). 头文件 /* 这是一份关于从Http请求信息中提取键值的接口声明的头文件 */ #ifndef _HEAD_H_ # ...
- [19/03/26-星期二] 容器_Map(图、键值对、映射)接口之HashMap(散列映射)&TreeMap(树映射)
一.概念&方法 现实生活中,我们经常需要成对存储某些信息.比如,我们使用的微信,一个手机号只能对应一个微信账户,这就是一种成对存储的关系. Map就是用来存储“键(key)-值(value) ...
- 【Java必修课】通过Value获取Map中的键值Key的四种方法
1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...
- 通过Value获取Map中的键值Key的四种方法
1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...
- Dictionary<k,v>键值对的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dict ...
- (转)C#中键值对类型Hashtable与Dictionary比较和相关用法
最近在使用C#中的Hashtable与Dictionary的时候,想知道其区别,通过查找网络相关博客资料,作出下列总结. Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是 ...
- c#Dictionary键值对的使用
直接粘代码吧 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
随机推荐
- Codeforces 757 D. Felicity's Big Secret Revealed 状压DP
D. Felicity's Big Secret Revealed The gym leaders were fascinated by the evolutions which took pla ...
- leetcode 659. Split Array into Consecutive Subsequences
You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...
- YTU 2892: I--免费看电影
2892: I--免费看电影 时间限制: 1 Sec 内存限制: 128 MB 提交: 30 解决: 12 题目描述 万象城星美影院开业1周年了,要举行为期一天的大酬宾,ACM队员准备去看电影.已 ...
- jQuery 与 AJAX 实现失去焦点验证用户名是否合格
JSP页面 <tr onmouseover="currentcolor=this.style.backgroundColor;this.style.backgroundColor='# ...
- ML.NET
ML.NET http://www.cnblogs.com/BeanHsiang/category/1218714.html 随笔分类 - 使用ML.NET实现NBA得分预测 摘要: 本文将介绍一种特 ...
- hdu 3333(树状数组 + 离线操作)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- UI:数据的解析XML与JSON
XML 和 JSON 语言 本篇博客来自互联网参考 XML 和 JSON 的互相转化 有属性的转化为对象,无属性的转化为字符串 节点的顺序性不可逆,XML有顺序,JSON 无顺序 XML 和 J ...
- Gym 100548F Color (数论容斥原理+组合数)
题意:给定 m 种颜色,把 n 盆花排成一直线的花涂色.要求相邻花的颜色不相同,且使用的颜色恰好是k种.问一共有几种涂色方法. 析:首先是先从 m 种颜色中选出 k 种颜色,然后下面用的容斥原理,当时 ...
- bzoj 1055: [HAOI2008]玩具取名【区间dp】
不难想,就是处理起来比较麻烦 设f[i][j][k]为是否可以把区间(i,j)合并为k,初始状态是f[i][j][s[i]]=1,转移的话另一段枚举长度x,向(i-x,j),(i,j+x)转移 把四个 ...
- JavaScript中this的使用方法总结
JavaScript中this的使用方法总结 在JavaScript中,this的使用分为四种场景,具体请参考阮一峰老师关于this的讲解 第一种情况是纯函数使用 var x =1 ; functio ...