为什么在c#中反序列化我的packages.config会返回null值

本文关键字:config 返回 null packages 我的 反序列化 为什么 | 更新日期: 2023-09-27 18:20:46

我正试图在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> 

为什么在c#中反序列化我的packages.config会返回null值

在这种情况下使用XmlElement:

[Serializable()]
[System.Xml.Serialization.XmlRoot("packages")]
public class PackageCollection
{
    [System.Xml.Serialization.XmlElement("package", typeof(Package))]
    public Package[] Packages { get; set; }
}

此代码似乎根据您的需求运行,基于所包含的测试:

[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模式定义工具(Xsd.exe)-MDSN可以提供帮助。

另请参阅问题如何反序列化XML文档以获取更多信息,特别是Marc Gravell的回答。