如何將XML與OBJECT進行相互轉換(泛型以及通用方法)
對于ORMCodeHelper(Keny的),完全的采用插件式開發,即插即用,個人感覺還是比較爽的,架構不錯。它包括了SQL SERVER 2000,SQL SERVER 2005以及ORACLE的相關C#代碼的生成。比哥一年前寫的那個牛多了,哈哈,哥去年亂寫了個網頁版的(http://www.cnblogs.com/jasenkin/archive/2010/02/11/1667511.html),現在看一年前的代碼,哥感嘆,這個誰寫的代碼,TMD實在寫的太爛了!!!當然,ORMCodeHelper與CodeSmith相比,還是有差距的哦。霖哥以前給我的codesmith模板(N層的),哥一直沒時間仔細看,哥知道那個模板可以把所有的代碼全部生成,其中包括N層代碼、存儲過程、頁面等等。雖然時間就像乳溝,只要擠一擠總還是有的!但是,哥真的......本來9號哥都是請假休息的,唉,又要哥上班了....
還有就是對于MVC3,Razor實在太給力了,扔掉MVC2吧,哈哈,@確實挺不錯的。
在ORMCodeHelper中,對于配置文件的使用的思路還是不錯的,哥學以致用,提煉個泛型的出來(其實最主要的還是插件開發的架構)。對于XML與OBJECT的轉換來說,下面講的是一種Serialize方法。其實哥還有另外一種通過反射將XML轉換成對象的方法,不過,涉及到公司****,那種方法還是不寫了。當然,那種方法哥是可以橫著寫了(因為哥早就背在心里了),哈哈,通用的代碼....
先看代碼,如下:
{
public static void Serialize<T>(string filePath, T[] array) where T:new()
{
if (string.IsNullOrEmpty(filePath)||
array == null||array.Length==0)
{
return;
}
try
{
XmlSerializerFactory xmlSerializerFactory = new XmlSerializerFactory();
XmlSerializer xmlSerializer =
xmlSerializerFactory.CreateSerializer(array.GetType(), typeof(T).Name);
Stream stream = new FileStream(filePath, FileMode.Create);
xmlSerializer.Serialize(stream, array);
stream.Close();
}
catch
{
}
}
public static void Serialize(string filePath, object obj)
{
if (string.IsNullOrEmpty(filePath) || obj == null)
{
return;
}
try
{
XmlSerializerFactory xmlSerializerFactory = new XmlSerializerFactory();
XmlSerializer xmlSerializer =
xmlSerializerFactory.CreateSerializer(obj.GetType(), obj.GetType().Name);
Stream stream = new FileStream(filePath, FileMode.Create);
xmlSerializer.Serialize(stream, obj);
stream.Close();
}
catch
{
}
}
}
{
List<T> results=new List<T>();
if (string.IsNullOrEmpty(filePath)||!File.Exists(filePath))
{
return results;
}
object obj = null;
try
{
XmlSerializerFactory xmlSerializerFactory = new XmlSerializerFactory();
XmlSerializer xmlSerializer =
xmlSerializerFactory.CreateSerializer(typeof(T[]), typeof(T).Name);
Stream stream = new FileStream(filePath, System.IO.FileMode.Open);
obj = xmlSerializer.Deserialize(stream);
stream.Close();
results.AddRange(obj as T[]);
}
catch
{
}
return results;
}
public static object Deserialize(string filePath, Type targetType)
{
if (string.IsNullOrEmpty(filePath)||!File.Exists(filePath)
|| targetType == null)
{
return null;
}
object obj = null;
try
{
XmlSerializerFactory xmlSerializerFactory = new XmlSerializerFactory();
XmlSerializer xmlSerializer =
xmlSerializerFactory.CreateSerializer(targetType, targetType.Name);
Stream stream = new FileStream(filePath, FileMode.Open);
obj = xmlSerializer.Deserialize(stream);
stream.Close();
}
catch
{
}
return obj;
}
從上面4個方法,可以看出主要是通過XmlSerializer將對象序列化為XML以及將XML反序列化為對象,這種方法比較簡單,而且易用。
(一)Serialize<T>(string filePath, T[] array),Deserialize<T>(string filePath)
通過單元測試來看看Serialize<T>(string filePath, T[] array)方法生成的XML內容,先注釋掉//DeleteFile(filePath);
public void SerializeTestHelper(AppSetting[] inputs)
{
AppSetting[] settings = inputs;
string filePath = @"d:\" + typeof(AppSetting).Name + ".config";
Serializer.Serialize<AppSetting>(filePath, settings);
List<AppSetting> results = Serializer.Deserialize<AppSetting>(filePath);
int length = results.Count;
Assert.IsTrue(length == settings.Length);
for (int index = 0; index < length; index++)
{
Assert.IsTrue(results[index].Value == settings[index].Value);
Assert.IsTrue(results[index].Key == settings[index].Key);
Assert.IsTrue(results[index].Author == settings[index].Author);
}
//DeleteFile(filePath);
}
生成的XML如下:
<ArrayOfAppSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AppSetting">
<AppSetting>
<Key>key0</Key>
<Value>value0</Value>
<Author>author0</Author>
</AppSetting>
<AppSetting>
<Key>key1</Key>
<Value>value1</Value>
<Author>author1</Author>
</AppSetting>
<AppSetting>
<Key>key2</Key>
<Value>value2</Value>
<Author>author2</Author>
</AppSetting>
</ArrayOfAppSetting>
從上面的單元測試可以看出:通過Serialize<T>(string filePath, T[] array)方法將對象數組生成XML內容,可以通過Deserialize<T>(string filePath)將XML內容轉換成相應的對象數組,內容相一致。
(二)Serialize(string filePath, object obj),Deserialize(string filePath, Type targetType)
通過單元測試來看看Serialize(string filePath, object obj)方法生成的XML內容,先注釋掉//DeleteFile(filePath);
private static void SerializeTestHelper()
{
AppSetting setting = new AppSetting()
{
Author = "AuthorTest",
Key = "KeyTest",
Value = "ValueTest"
};
string filePath = @"d:\" + typeof(AppSetting).Name + ".config";
Serializer.Serialize(filePath, setting);
AppSetting result = Serializer.Deserialize(filePath, typeof(AppSetting)) as AppSetting;
Assert.IsTrue(result.Value == setting.Value);
Assert.IsTrue(result.Author == setting.Author);
Assert.IsTrue(result.Key == setting.Key);
//DeleteFile(filePath);
}
生成的XML如下:
<AppSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AppSetting">
<Key>KeyTest</Key>
<Value>ValueTest</Value>
<Author>AuthorTest</Author>
</AppSetting>
從上面的單元測試可以看出:通過Serialize(string filePath, object obj)方法將對象生成XML內容,可以通過Deserialize(string filePath, Type targetType)將XML內容轉換成相應的對象,內容相一致。其中,object也可以是對象數組的,這個留給讀者自己去驗證。
測試都是可以通過的,這里僅僅是驗證正確的功能,如下圖: