XML如何序列化成字符串

来源:互联网 发布:淘宝商品详情页制作 编辑:程序博客网 时间:2024/04/29 21:56

XML如何序列化成字符串 

-----------------------------

using System;
using System.Xml.Serialization;

namespace BLL.FrameWork
{
 /// <summary>
 /// ActionMapping对Struts-config.xml的映射 的摘要说明。
 /// 2005-12-11 PM 17:15 Modify HHD.
 /// </summary>
 [XmlRoot("action-mappings")]
 public class ActionMapping
 {
  [XmlElement("action")]
   public Action[] Actions;
 }

 public class Action
 {
  [XmlAttribute("name")]
  public string Name = "";
  [XmlAttribute("classname")]
  public string ClassName = "";
  [XmlAttribute("path")]
  public string Path = "";
  [XmlAttribute("input")]
  public string Input = "";
  [XmlAttribute("validate",DataType="boolean")]
  public bool Validate = false;
  [XmlAttribute("istransaction",DataType="boolean")]
  public bool IsTransAction = false;
  [XmlElement("forward")]
  public Forward[] Forwords;
 }

 public class Forward
 {
  [XmlAttribute("name")]
  public string Name = "";
  [XmlAttribute("path")]
  public string Path = "";
 }
}

调用

。。。

Hashtable StrutsData = (Hashtable)HttpContext.Current.Cache["StrutsData"];
    if(StrutsData==null)
    {
     StrutsData = new Hashtable();
     string xmlpath = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["strutsconfig"]);
     XmlSerializer xs = new XmlSerializer(typeof(ActionMapping));
     FileStream fs = new FileStream(xmlpath,FileMode.Open);
     ActionMapping actionmapping = (ActionMapping)xs.Deserialize(fs);
     fs.Close();
     int actionmappinglength = actionmapping.Actions.Length;
     
     for(i=0;i<actionmappinglength;i++)
     {
      Hashtable temp = new Hashtable();
      temp.Add("ClassName",actionmapping.Actions[i].ClassName);
      temp.Add("Path",actionmapping.Actions[i].Path);
      temp.Add("Input",actionmapping.Actions[i].Input);      
      temp.Add("Validate",actionmapping.Actions[i].Validate);
      temp.Add("IsTransAction",actionmapping.Actions[i].IsTransAction);
      temp.Add("Forwords",actionmapping.Actions[i].Forwords);
      StrutsData.Add(Convert.ToString(actionmapping.Actions[i].Name),temp);      
     }
     HttpContext.Current.Cache.Insert("StrutsData",StrutsData);
    }
。。。

原创粉丝点击