这篇文章主要为大家详细介绍了PHP利用XML与对象的序列化与反序列化的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!
这篇文章主要讲述XML与对象的序列化与反序列化。并且会附上一些简单的序列化与反序列化方法,供大家使用。
假设我们在一个Web项目中有这样两个类
代码如下:
public class Member
{
public string Num { get; set; }
public string Name { get; set; }
}
public class Team
{
public string Name;
public List<Member> Members { get; set; }
}
代码如下:
<!--使用Razor来演示-->
@model Team
<form id="submitForm" action="http://www.johnconnor.com/team" method="post">
<input type="hidden" name="TeamName" value="@Model.Name" />
<input type="hidden" name="MemberNum1" value="@Model.Members[0].Num" />
<input type="hidden" name="MemberName1" value="@Model.Members[0].Name" />
...
<!--省略28X2个input标签-->
<input type="hidden" name="MemberNum30" value="@Model.Members[29].Num" />
<input type="hidden" name="MemberName30" value="@Model.Members[29].Name" />
</form>
<script type="text/javascript">
document.getElementById("submitForm").submit();
</script>
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Team xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Development</Name>
<Members>
<Member>
<Num>001</Num>
<Name>Marry</Name>
</Member>
<Member>
<Num>002</Num>
<Name>John</Name>
</Member>
</Members>
</Team>
代码如下:
/// <summary>
/// 提供xml文档序列化 反序列化
/// </summary>
public sealed class EncodeHelper
{
/// <summary>
/// 反序列化XML字符串为指定类型
/// </summary>
public static object Deserialize(string Xml, Type ThisType)
{
XmlSerializer xmlSerializer = new XmlSerializer(ThisType);
object result;
try
{
using (StringReader stringReader = new StringReader(Xml))
{
result = xmlSerializer.Deserialize(stringReader);
}
}
catch (Exception innerException)
{
bool flag = false;
if (Xml != null)
{
if (Xml.StartsWith(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble())))
{
flag = true;
}
}
throw new ApplicationException(string.Format("Couldn't parse XML: '{0}'; Contains BOM: {1}; Type: {2}.",
Xml, flag, ThisType.FullName), innerException);
}
return result;
}
/// <summary>
/// 序列化object对象为XML字符串
/// </summary>
public static string Serialize(object ObjectToSerialize)
{
string result = null ;
try
{
XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
using (MemoryStream memoryStream = new MemoryStream())
{
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding(false));
xmlTextWriter.Formatting = Formatting.Indented;
xmlSerializer.Serialize(xmlTextWriter, ObjectToSerialize);
xmlTextWriter.Flush();
xmlTextWriter.Close();
UTF8Encoding uTF8Encoding = new UTF8Encoding(false, true);
result= uTF8Encoding.GetString(memoryStream.ToArray());
}
}
catch (Exception innerException)
{
throw new ApplicationException("Couldn't Serialize Object:" + ObjectToSerialize.GetType().Name, innerException);
}
return result;
}
}
代码如下:
static void Main(string[] args)
{
List<Member> Members = new List<Member>();
Member member1 = new Member { Name = "Marry", Num = "001" };
Member member2 = new Member { Name = "John", Num = "002" };
Members.Add(member1);
Members.Add(member2);
Team team = new Team { Name = "Development", Members = Members };
var xml =EncodeHelper.Serialize(team);//序列化
Console.Write(xml);//打印序列化后的XML字符串
Console.ReadLine();
Team newTeam = EncodeHelper.Deserialize(xml, typeof(Team)) as Team;//反序列化时需要显式的进行类型转换
Console.WriteLine("Team Name:"+newTeam.Name);//显示反序列化后的newTeam对象
foreach (var member in newTeam.Members)
{
Console.WriteLine("Member Num:" + member.Num);
Console.WriteLine("Member Name:" + member.Name);
}
Console.ReadLine();
}
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Team xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Development</Name>
<Members>
<Member>
<Num>001</Num>
<Name>Marry</Name>
</Member>
<Member>
<Num>002</Num>
<Name>John</Name>
</Member>
</Members>
</Team>
代码如下:
public class Member
{
[XmlElement("Member_Num")]
public string Num { get; set; }
public string Name { get; set; }
}
[XmlRoot("Our_Team")]
public class Team
{
[XmlIgnore]public string Name;
public List<Member> Members { get; set; }
}
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Our_Team xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Members>
<Member>
<Member_Num>001</Member_Num>
<Name>Marry</Name>
</Member>
<Member>
<Member_Num>002</Member_Num>
<Name>John</Name>
</Member>
</Members>
</Our_Team>
本文来自:http://www.q1010.com/173/15529-0.html
注:关于PHP利用XML与对象的序列化与反序列化的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:XML
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。