C#序列化对象转为为XML格式字符串

实习公司原来的左侧菜单是通过js进行ajax请求一个xml文件得到一个xml对象,然后拼接html文件,现在需要从数据库取数据生成xml,因为保密关系,太详细的不能放出来,简单说下流程

1、C#后台执行SQL,得到DataTable对象

2、变量DataTable对象,生成一个Dictionary,key就是主菜单,value就是这个主菜单包含的所有子菜单,是个ArrayList

3、变量这个Dictionary,根据每个key生成一个对象,这个对象的属性是个List,List中包含的是子菜单的对象

4、序列化这些对象,生成xml,转为字符串传给aspx

出于保密需要,我把问题抽象一下,这个xml可以用国家-省-城市来表达

从数据库取出的数据差不多长这样(这是多表join后的结果集):

idprovincecityaddress
1江苏南京nanjing
2浙江杭州hangzhou
3浙江宁波ningbo

最后需要生成的xml长这样:

<?xml version="1.0" encoding="utf-16"?>
<country xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" width="200">
  <province caption="Summary">
    <city name="南京" address="nanjing"/>
  </province>
  <province caption="jiangsu">
    <city name="杭州" address="hangzhou"/>
    <city name="宁波" address="ningbo"/>
  </city>
</country>  

首先,定义类

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using IAUOperation.db;
using System.Collections;
using System.Data.SqlClient;
using Operation.Web;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("country")]
public class Country
{
    [XmlAttribute]
    public string width;

    [XmlElement("province")]
    public List<Province> provinces;

}


[XmlRoot("province")]
public class Province
{
    [XmlAttribute]
    public string name;

    [XmlElement("city")]
    public List<City> cities;

}


public class City
{
    [XmlAttribute]
    public string name;

    [XmlAttribute]
    public string address;
}


public class CityItem
{
    public string province;

    public string city;

    public string address;
}

注意,这里保存主菜单和子菜单的对应关系时,不能用Hashtable,因为Hashtable是无序存储的,而菜单是必须有顺序的 

这里只给出了核心代码作为示例

DataTable dt = dbBase.executeInternalQuery(strSQL);

HashSet<string> hashSetMenus = new HashSet<string>();


Dictionary<string, ArrayList> provinceCity = new Dictionary<string, ArrayList>();

List<SubMenuItem> CityItemList = new List<SubMenuItem>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    string tmpProvince = dt.Rows[i]["province"].ToString();
    hashSetProvinces.Add(tmpProvince);

    CityItem cityItem = new SubMenuItem();
    cityItem.province = tmpProvince;
    cityItem.city = dt.Rows[i]["city"].ToString();
    cityItem.address = dt.Rows[i]["address"].ToString();
    CityItemList.Add(cityItem);
}

for (int i = 0; i < dt.Rows.Count; i++)
{

    // 省
    string tmpProvince = dt.Rows[i]["province"].ToString();
    // 市
    string tmpCity = dt.Rows[i]["city"].ToString();

    if (hashSetProvinces.Contains(tmpMainMenu))
    {
        if (!provinceCity.ContainsKey(tmpMainMenu))
        {
            ArrayList citylist = new ArrayList();
            citylist.Add(tmpSubMenu);
            provinceCity.Add(tmpProvince, citylist);
        }
        else
        {
            ArrayList temp = provinceCity[tmpProvince] as ArrayList;
            temp.Add(tmpCity);

        }
    }

}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值