XML 物件系列化與反系列化

2021-06-18 13:37:10 字數 3527 閱讀 9799

using system;

using system.collections.generic;

using system.web;

using system.text;

using system.reflection;

using system.xml;

using system.xml.serialization;

using system.io;

using system.xml.schema;

namespace erp.webclient.common

utf8encoding encoding = new utf8encoding(false);

xmlserializer serializer = new xmlserializer(type);

memorystream stream = new memorystream();

xmltextwriter writer = new xmltextwriter(stream, encoding);

writer.formatting = (tobeindented ? formatting.indented : formatting.none);

serializer.serialize(writer, obj);

string xml = encoding.getstring(stream.toarray());

writer.close();

return xml;

}/// ///

///

///

///

///

public static object xmltoobject(string xml, type type)

object o = null;

xmlserializer serializer = new xmlserializer(type);

stringreader strreader = new stringreader(xml);

xmlreader reader = new xmltextreader(strreader);

trycatch (invalidoperationexception e)

catch (exception e)

finally

return o;

}/// /// 實體類序列化成xml

///

/// 實體.

/// 節點名稱

///

public static string objectlisttoxml(listenitities, string headtag)

foreach (propertyinfo propinfo in propinfos)

}return sb.tostring();

}//xml轉行為實體類

/// /// xml檔案轉化為實體類列表

///

/// 實體名稱

/// xml檔案

/// xml標頭檔案

/// 實體列表

public static listxmltoobjectlist(string xml, string headtag) where t : new()

//填充entity類的屬性

foreach (propertyinfo propinfo in propinfos)

}list.add(entity);

}return list;

}#endregion

}}

3.  類的設計

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.componentmodel;

using system.xml.serialization;

using system.servicemodel;

using system.runtime.serialization;

namespace glaciericr.common.workdatatypes

//[datacontract(namespace = "")]

[xmlinclude(typeof(clientuitype))]

[knowntype(typeof(clientuitype))]

public class clientuiconfigsetting

public bool hasremarks

public listclientuitypesetcollection

}[xmlinclude(typeof(singleoption))]

[xmlinclude(typeof(muloption))]

[xmlinclude(typeof(dateselection))]

[xmlinclude(typeof(singlelinetextbox))]

[xmlinclude(typeof(mullinetextbox))]

[xmlinclude(typeof(decimaltextbox))]

[knowntype(typeof(singleoption))]

[knowntype(typeof(muloption))]

[knowntype(typeof(dateselection))]

[knowntype(typeof(singlelinetextbox))]

[knowntype(typeof(mullinetextbox))]

[knowntype(typeof(decimaltextbox))]

public abstract class clientuitype

}public class singleoption : clientuitype

public bool hasother

}public class muloption : clientuitype

public bool hasother

}public class dateselection : clientuitype

public class singlelinetextbox : clientuitype

}public class mullinetextbox : clientuitype

}public class decimaltextbox : clientuitype

public decimal maxvalue

public string unit

public decimallimittype mindecimallimittype

public decimallimittype maxdecimallimittype

}}

C 實現XML系列化和反系列化的總結

常用的系列化定義 using system.xml.serialization xmlattribute name 定義 xmlelement label 定義 xmlignoreattribute 跳過系列化 xmlelement description isnullable false 定義 在...

系列化與反序列化分析

c 中的 序列化 serializable 理解 我的理解 比如用乙個類描述一張合同,而這個類例項化後其中的字段儲存著合同的資訊,如果現在要把這個類的例項傳送到另一台機器 另乙個窗體或是想儲存這個類以便以 後再取出來用 持久化物件 可以對這個類進行序列化 序列化實際上是乙個資訊流 傳送或儲存,用的時...

物件,物件集合的簡單Xml序列化與反序列化

物件,物件集合的簡單xml序列化與反序列化 本文描述如何使用clr中的 stringwriter,xmlserializer將物件,物件集合 序列化為xml格式的字串,同時描述如何進行 反序列化.c 版本 c 3.0 開發環境 vs 2008 主要方法 複製複製 複製儲存 usingsystem u...