XML如何序列化成字符串

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);
    }
。。。

在C#中,将类序列化XML字符串可以通过使用.NET Framework提供的`System.Xml.Serialization`命名空间中的类来实现。序列化是将对象的状态信息转换为可以存储或传输的格式的过程,在.NET中常见的序列化方式包括二进制序列化、SOAP序列化以及XML序列化XML序列化是一种将对象转换为XML格式文档的过程,通常用于数据交换。 以下是一个使用`XmlSerializer`类将类序列化XML字符串的基本步骤: 1. 首先,需要在类定义上使用`[XmlRoot]`属性来指定类名,以及在类的员变量上使用`[XmlElement]`属性来指定员变量对应的XML标签名。 2. 创建`XmlSerializer`的实例,传入类的类型作为参数。 3. 使用`XmlSerializer`的`Serialize`方法将对象序列化XML,然后使用`XmlWriter`将序列化内容写入字符串。 4. 如果需要反序列化,可以使用`Deserialize`方法从XML字符串中还原对象。 示例代码如下: ```csharp using System; using System.IO; using System.Xml.Serialization; public class Person { [XmlElement] public string Name { get; set; } [XmlElement] public int Age { get; set; } [XmlElement] public string City { get; set; } } public class SerializationHelper { public static string SerializeObjectToXmlString<T>(T objectToSerialize) { var xmlSerializer = new XmlSerializer(typeof(T)); using (var textWriter = new StringWriter()) { xmlSerializer.Serialize(textWriter, objectToSerialize); return textWriter.ToString(); } } } // 使用示例 class Program { static void Main(string[] args) { var person = new Person { Name = "张三", Age = 25, City = "北京" }; string xmlString = SerializationHelper.SerializeObjectToXmlString(person); Console.WriteLine(xmlString); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值