数据字典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 ...
随机推荐
- Cordova打包vue项目(Android)
准备工作: 安装好必要环境: vue-cli,webpack,node.js,android环境 (http://cordova.axuer.com/docs/zh-cn/latest/guide/p ...
- hibernate could not resolve property
映射文件字段不匹配 这个异常一般是因为映射文件造成的: 1.hibernate.config.xml中添加的映射文件是否正确 2.映射文件中的类是否正确指定. 3.映射文件映射的属性名是否跟类中的属性 ...
- SPOJ:Labyrinth(最大直线)
The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is d ...
- VS2013插件开发
一.功能描述 项目中有一个AppSettings.xml文件,当这个文件有添加新元素的时候,将该添加的内容自动同步到其他目录下的AppSettings.xml文件. 二.插件模板选择 要开发VS插件需 ...
- 小程序-demo:小熊の日记
ylbtech-小程序-demo:小熊の日记 1.CHANGELOG.md # -- * 更新开发者工具至`v0.10.101100` * 修改`new`页的数据绑定方式 & 修改多行文本框输 ...
- VScode相关
这就是我想要的 VSCode 插件! VS Code 快捷键(中英文对照版) visual studio code 配置vue开发环境 vscode 这样的注释怎么生成? 能让你开发效率翻倍的 VSC ...
- Thinkpad x230i安装Ubuntu10.04发生no network devices available
这个是由于10.04版本没有集成x230i的网卡驱动导致,需要到http://sourceforge.net/projects/e1000/f ... %20stable/下载最新版本驱动,并安装,之 ...
- Postgresql空库发布或者部分空库,模式,表处理备份流程
--备份数据库结构pg_dump -h localhost -p 8101 -d qhsoam -s -f /tmp/nodata.sql --仅备份某模式和该模式下的数据表结构pg_dump -h ...
- node.js在读取文件时中文乱码问题
断更很久了........从今天开始会努力的持续更博,积极学习. 言归正传.今天在写node.js的demo时发现一个bug.我在node中读取本地的text文件时,发现英文的内容可以被读取,但是中文 ...
- zoj 2760 How Many Shortest Path【最大流】
不重叠最短路计数. 先弗洛伊德求一遍两两距离(其实spfa或者迪杰斯特拉会更快但是没必要懒得写),然后设dis为st最短距离,把满足a[s][u]+b[u][v]+a[v][t]==dis的边(u,v ...