我试图在C#中反序列化我的packages.config文件,但我回来的Collection总是为null。如果我的xml文件由单个属性集合组成,是否需要特殊的东西?
[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();
}
我的packages.config看起来像
<?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>
答案 0 :(得分:2)
在这种情况下使用XmlElement:
[Serializable()]
[System.Xml.Serialization.XmlRoot("packages")]
public class PackageCollection
{
[System.Xml.Serialization.XmlElement("package", typeof(Package))]
public Package[] Packages { get; set; }
}
答案 1 :(得分:2)
此代码似乎根据您的需要根据所包含的测试运行:
[Serializable()]
[System.Xml.Serialization.XmlTypeAttribute()]
public class Package
{
[System.Xml.Serialization.XmlAttributeAttribute("id")]
public string Id { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute("version")]
public string Version { get; set; }
[System.Xml.Serialization.XmlAttributeAttribute("targetFramework")]
public string TargetFramework { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("packages")]
public class PackageCollection
{
[System.Xml.Serialization.XmlElementAttribute("package")]
public Package[] Packages { get; set; }
}
class Program
{
static void Main()
{
var path = @"C:\packages.config";
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(PackageCollection));
StreamReader reader = new StreamReader(path);
var packages2 = (PackageCollection)serializer.Deserialize(reader);
foreach (Package pkg in packages2.Packages)
{
Console.WriteLine("ID: " + pkg.Id);
Console.WriteLine("Version: " + pkg.Version);
Console.WriteLine("Target Framework: " + pkg.TargetFramework);
Console.WriteLine();
}
reader.Close();
Console.ReadLine();
}
}
正如@Isantipov所提到的,XML Schema Definition Tool (Xsd.exe)- MDSN可以提供帮助。
另请参阅问题How to Deserialize XML document以获取更多信息,特别是Marc Gravell's answer。