I am trying to deserialize my package.config file in C #, but the collection I get is always null. Is there anything special that is needed if my XML file consists of one set of attributes?
[Serializable()] [System.Xml.Serialization.XmlTypeAttribute()] public class Package { [System.Xml.Serialization.XmlAttribute("id")] public string Id {get;set;} [System.Xml.Serialization.XmlAttribute("version")] public string Version {get;set;} } [Serializable()] [System.Xml.Serialization.XmlRoot("packages")] public class PackageCollection { [System.Xml.Serialization.XmlArrayItem("package", typeof(Package))] public Package[] Packages {get;set;} } void Main() { var path = "C:\D\packages.config"; var serializer = new System.Xml.Serialization.XmlSerializer(typeof(PackageCollection), new System.Xml.Serialization.XmlRootAttribute("packages")); StreamReader reader = new StreamReader(file); var packages2 = (PackageCollection)serializer.Deserialize(reader); reader.Close(); }
where my packages.config looks like
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Autofac" version="3.3.1" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi.Tracing" version="4.0.30506" targetFramework="net45" /> <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /> <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" /> </packages>
source share