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存放键值对的更多相关文章

  1. 04.Dictionary字典键值对集合

    Dictionary字典键值对集合和Hashtable键值对集合的功能非常类似, 只是在声明的时候,必须为其制定值的类型. 示例代码: namespace _11.Dictionary字典集合的学习 ...

  2. Dictionary读取键值的快捷方法

    对泛型集合Dictionary<T,T> 进行读取键值是经常的操作,一般情况下,都是通过keys 和values进行键值的读取操作: eg: foreach (var item in di ...

  3. C语言定义从URL中获取键值的接口

    环境:centos7下,对客户端http请求进行解析,来获取有效键值(包括汉字). 头文件 /* 这是一份关于从Http请求信息中提取键值的接口声明的头文件 */ #ifndef _HEAD_H_ # ...

  4. [19/03/26-星期二] 容器_Map(图、键值对、映射)接口之HashMap(散列映射)&TreeMap(树映射)

    一.概念&方法 现实生活中,我们经常需要成对存储某些信息.比如,我们使用的微信,一个手机号只能对应一个微信账户,这就是一种成对存储的关系. Map就是用来存储“键(key)-值(value) ...

  5. 【Java必修课】通过Value获取Map中的键值Key的四种方法

    1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...

  6. 通过Value获取Map中的键值Key的四种方法

    1 简介 我们都知道Map是存放键值对<Key,Value>的容器,知道了Key值,使用方法Map.get(key)能快速获取Value值.然而,有的时候我们需要反过来获取,知道Value ...

  7. Dictionary<k,v>键值对的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dict ...

  8. (转)C#中键值对类型Hashtable与Dictionary比较和相关用法

    最近在使用C#中的Hashtable与Dictionary的时候,想知道其区别,通过查找网络相关博客资料,作出下列总结. Hashtable与Dictionary虽然都是作为键值对的载体,但是采用的是 ...

  9. c#Dictionary键值对的使用

    直接粘代码吧 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

随机推荐

  1. 2016/4/7 datatype:①json ②XML

    ①JSON 1,postjsonxml.php     json用循环方式处理传来的值  for(key in data) for(var i=0;i<data.length;i++){data ...

  2. java8--异常处理(java疯狂讲义3复习笔记)

    try,catch,finally,throw,throws java将异常分为两种,Checked异常和Runtime异常. IndexOutOfBoundsException NumberForm ...

  3. Tomcat 安装错误

    安装tomcat时,遇到"failed to install tomcat6 service check your settings and permissions"的问题 安装时 ...

  4. FOUNDATION OF ASYNCHRONOUS PROGRAMMING

    The async and await keywords are just a compiler feature. The compiler creates code by using the Tas ...

  5. POJ3186 Treats for the Cows —— DP

    题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K To ...

  6. YTU 2904: B--Faultfinding

    2904: B--Faultfinding 时间限制: 1 Sec  内存限制: 128 MB 提交: 64  解决: 33 题目描述 Do you remember the game in whic ...

  7. evm指令集手册

    evm指令集手册 Opcodes 结果列为"-"表示没有运算结果(不会在栈上产生值),为"*"是特殊情况,其他都表示运算产生唯一值,并放在栈顶. mem[a.. ...

  8. [HEOI 2012] 采花

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2743 [算法] 首先预处理nxt[]数组 , 其中 , nxt[i]表示下一个和i号 ...

  9. LA 4670 Dominating Patterns (AC自动机)

    题意:给定一个一篇文章,然后下面有一些单词,问这些单词在这文章中出现过几次. 析:这是一个AC自动机的裸板,最后在匹配完之后再统计数目就好. 代码如下: #pragma comment(linker, ...

  10. PCB Web版SI9000阻抗计算器

    在几个月前写过一遍关于: PCB SI9000阻抗计算引擎Web方式实现方法  ,最近开始参考Polar SI9000的界面,将阻抗计算器转为网页版的方式实现.   一.Web版SI9000阻抗计算器 ...