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. CASE UPDATE

    https://leetcode-cn.com/problems/swap-salary/description/ Given a table salary, such as the one belo ...

  2. spring boot自定义properity

    1.spring boot使用application.properties默认了很多配置. 但有时需要自定义配置.若在application.properties添加属性: app.name=fish ...

  3. vmware中鼠标在部分区域不能使用

    https://blog.csdn.net/dreamengsoul/article/details/80439278 方法1:重装VMware tools; 方法2:使用增强型键盘鼠标: 方法3:检 ...

  4. Can't remove netstandard folder from output path (.net standard)

    https://developercommunity.visualstudio.com/content/problem/30940/cant-remove-netstandard-folder-fro ...

  5. Mybatis拦截器(插件实现原理)

    在mybatis的mybatis.cfg.xml中插入: <plugins> <plugin interceptor="cn.sxt.util.PageIntercepto ...

  6. Net4.6 Task 异步函数 比 同步函数 慢5倍 踩坑经历

    Net4.6 Task 异步函数 比 同步函数 慢5倍 踩坑经历 https://www.cnblogs.com/shuxiaolong/p/DotNet_Task_BUG.html 异步Task简单 ...

  7. eclipse下清除项目的svn信息

    点击项目右键->Team->Disconnect 选择第一个即可

  8. 警告框在asp.net mvc中应用

    Bootstrap与asp.net MVC框架结合,产生警告框.主要是利用控制器的TempData 字典对象 生成临时数据. ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个 ...

  9. Python 元组、列表

    Python中,最常用的数据结构就是序列了.序列有以下几种:元组,列表,字符串等.注:代码都是在IDE中编辑的,没有用交互式环境. 一.元组(tuple) 元组其实有点类似C++中的数组,即一旦指定了 ...

  10. Quartz.NET 快速入门

    官网:http://www.quartz-scheduler.net/ API:http://www.quartz-scheduler.net/documentation/index.html 快速入 ...