如何將.net對象序列化爲XML,然後將其反序列化?如何將對象序列化爲XML
-1
A
回答
3
在VB.Net你首先一些屬性添加到您的類(如果需要)
Public Class Class1
<XmlAttribute("value")>
Public Property Value As String
<XmlElement("name")>
Public Property Name As String
End Class
,然後使用
Dim p As New Class1()
p.Name = "test"
Dim sw1 = New StringWriter()
Dim xs1 As New XmlSerializer(GetType(Class1))
xs1.Serialize(New XmlTextWriter(sw1), p)
Console.WriteLine(sw1.ToString())
4
這是可能的使用對象和字符串擴展方法序列化。
using System.IO;
using System.Xml.Serialization;
namespace XmlExtensionMethods
{
/// <summary>
/// Provides extension methods useful for XML Serialization and deserialization.
/// </summary>
public static class XMLExtensions
{
/// <summary>
/// Extension method that takes objects and serialized them.
/// </summary>
/// <typeparam name="T">The type of the object to be serialized.</typeparam>
/// <param name="source">The object to be serialized.</param>
/// <returns>A string that represents the serialized XML.</returns>
public static string SerializeXML<T>(this T source) where T : class, new()
{
source.CheckNull("Object to be serialized.");
var serializer = new XmlSerializer(typeof(T));
using (var writer = new StringWriter())
{
serializer.Serialize(writer, source);
return writer.ToString();
}
}
/// <summary>
/// Extension method to string which attempts to deserialize XML with the same name as the source string.
/// </summary>
/// <typeparam name="T">The type which to be deserialized to.</typeparam>
/// <param name="XML">The source string</param>
/// <returns>The deserialized object, or null if unsuccessful.</returns>
public static T DeserializeXML<T>(this string XML) where T : class, new()
{
XML.CheckNull("XML-string");
var serializer = new XmlSerializer(typeof(T));
using (var reader = new StringReader(XML))
{
try { return (T)serializer.Deserialize(reader); }
catch { return null; } // Could not be deserialized to this type.
}
}
}
}
}
你可以使用這樣的:
string savestring;
public void Save()
{
savestring = mySerializableObject.SerializeXML<MySerializableClass>();
}
public void Load()
{
mySerializableObject = savestring.DeserializeXML<MySerializableClass>();
}
PS:我不邀功這些擴展方法。
+1
偉大的答案每個人。謝謝! – jdross
0
VB.NET
Public Shared Function SerializeObj(obj As Object) As String
Dim xs As New System.Xml.Serialization.XmlSerializer(obj.GetType)
Dim w As New IO.StringWriter
xs.Serialize(w, obj)
Return w.ToString
End Function
讓我們回到一個稍微複雜一點,你將宣佈一個XML文檔和提取數據出來這一點。
相關問題
- 1. 將對象序列化爲XML
- 2. C#將XML反序列化爲對象
- 3. 將python對象序列化爲XML
- 4. 將對象序列化爲JSON,XML,YAML?
- 5. 將對象序列化爲XML Java
- 6. 將XML反序列化爲C#對象
- 7. 將xml反序列化爲對象
- 8. 將對象序列化爲XML
- 9. 將XML反序列化爲C#對象
- 10. 對象序列化爲JSON,如何序列化屬性(如XML)
- 11. 如何將對象序列化爲XDocument?
- 12. 如何將對象序列化爲JSON?
- 13. 如何將自定義對象序列化並反序列化爲xml?
- 14. 如何將xml反序列化爲繼承對象?
- 15. 如何將Moose對象序列化爲XML?
- 16. 如何將內部XML反序列化爲對象?
- 17. 如何將匿名類型對象序列化爲XML?
- 18. 如何將xstream轉換爲java對象的xml序列化?
- 19. 如何將xml反序列化爲對象數組?
- 20. 如何將包含ILIST成員的對象序列化爲XML?
- 21. XML序列化:對象未序列化
- 22. 如何序列化對象到XML?
- 23. 如何反序列化XML到對象
- 24. 將Java對象序列化爲xml和後面(XML配置)
- 25. Android XML對象序列化
- 26. Xml序列化對象(HttpBrowserCapabilities)
- 27. XML對象序列化
- 28. 序列化對象到XML
- 29. 反序列化XML響應爲對象
- 30. 如何在將對象序列化爲XML時添加XML名稱空間(xmlns)
http://stackoverflow.com/search?q=%5Bc%23%5D+serialize+object+xml – dtb
可以通過這個鏈接,它可以幫助 http://stackoverflow.com/questions/4143421 /最快的方式來序列化和反序列化網絡對象 –