假設你CatalogProduct
物體看起來是這樣的:
public class CatalogProduct {
public string Name;
public string Version;
}
我想的LINQ to XML將是最簡單和最快的方式爲您
var cps1 = new[] { new CatalogProduct { Name = "Name 1", Version = "Version 1" },
new CatalogProduct { Name = "Name 2", Version = "Version 2" } };
var xml = new XElement("CatalogProducts",
from c in cps1
select new XElement("CatalogProduct",
new XAttribute("Name", c.Name),
new XAttribute("Version", c.Version)));
// Use the following to deserialize you objects
var cps2 = xml.Elements("CatalogProduct").Select(x =>
new CatalogProduct {
Name = (string)x.Attribute("Name"),
Version = (string)x.Attribute("Version") }).ToArray();
請注意,.NET提供了真正的對象圖序列化,我沒有顯示
這不是一個反序列化,因爲它不是序列化的結果。我可以說這只是一個解析或創建一個基於xml(你自己的結構)的對象。不是嗎? – abatishchev 2010-11-09 15:22:58