反序列化xml时嵌套元素中为null

时间:2019-05-16 04:36:15

标签: c# xml deserialization xml-deserialization

我正在将我的字符串化xml反序列化到我的类中。 问题在于嵌套的子元素或其属性的一部分返回null。

这是我的反序列化功能:

public static T DeSerialize<T>(this string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    using (TextReader reader = new StringReader(xml))
    {
        T result = (T)serializer.Deserialize(reader);
        return result;
    }
}

这是我的课程:

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.specifiedcompany.com/API")]
public partial class VideoGames
{
    private GameType[] typesField;

    private Platform platformField;

    private string memoField;

    [System.Xml.Serialization.XmlArray("Types", Order = 0)]
    [System.Xml.Serialization.XmlArrayItem("Data", IsNullable = false)]
    public GameType[] Types
    {
        get
        {
            return this.typesField;
        }
        set
        {
            this.typesField= value;
        }
    }

    [System.Xml.Serialization.XmlElementAttribute("Platform", Order = 1)]
    public Platform Platform
    {
        get
        {
            return this.platformField;
        }
        set
        {
            this.platformField= value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Memo
    {
        get
        {
            return this.memoField;
        }
        set
        {
            this.memoField= value;
        }
    }
}

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.specifiedcompany.com/API")]
public partial class Platform
{
    private decimal compressedField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal Compressed
    {
        get
        {
            return this.compressedField;
        }
        set
        {
            this.compressedField= value;
        }
    }
}

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.specifiedcompany.com/API")]
public partial class GameType
{
    private Code[] codeField;

    private string nameField;

    [System.Xml.Serialization.XmlArrayAttribute("Code", Order = 0)] 
    [System.Xml.Serialization.XmlArrayItemAttribute("Category", IsNullable = false)]
    public Code[] Code
    {
        get
        {
            return this.codeField;
        }
        set
        {
            this.codeField= value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField= value;
        }
    }
}

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.specifiedcompany.com/API")]
public partial class Code
{
    private string textField;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Text
    {
        get
        {
            return this.textField;
        }
        set
        {
            this.textField= value;
        }
    }
}

这是xml:

<VideoGames Memo="1">
    <Types>
        <Data Name="RPG">
            <Code>
                <Category Text="TestingData"/>
            </Code>
        </Data>
    </Types>
    <Platform Compressed="3.2876"/>
</VideoGames>

我正在像这样使用反序列化:

string xml = "";//xml string above.
VideoGames a = xml.DeSerialize<VideoGames>();

我希望类中的每个属性都具有价值,但是只有VideoGames中的Memo才有价值,其他属性都为空。

例如:a.Types为空,但是a.Memo为“ 1”。

我已经尝试过通过本主题搜索的所有相关问题,但没有一个起作用。

注意:

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xml);

我已经检查了此xDoc,它已完美加载,没有丢失任何内容。

1 个答案:

答案 0 :(得分:0)

诊断XML反序列化中问题的最简单方法是序列化为XML ,然后将观察到的XML与所需的XML进行比较。通常问题会立即显现。

如果我按照以下方式创建和序列化VideoGames的实例:

var root = new VideoGames
{
    Types = new []
    {
        new GameType
        {
            Name = "RPG",
            Code = new []
            {
                new Code { Text = "TestingData" },
            }
        },
    },
    Platform = new Platform  { Compressed = 3.2876m, },
};

var xml = root.GetXml(omitStandardNamespaces: true);
Console.WriteLine(xml);

使用以下扩展方法:

public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
{
    XmlSerializerNamespaces ns = null;
    if (omitStandardNamespaces)
    {
        ns = new XmlSerializerNamespaces();
        ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
    }           
    using (var textWriter = new StringWriter())
    {
        var settings = new XmlWriterSettings() { Indent = true }; // For cosmetic purposes.
        using (var xmlWriter = XmlWriter.Create(textWriter, settings))
            (serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
        return textWriter.ToString();
    }
}

然后输出为:

<VideoGames>
  <Types xmlns="http://www.specifiedcompany.com/API">
    <Data Name="RPG">
      <Code>
        <Category Text="TestingData" />
      </Code>
    </Data>
  </Types>
  <Platform Compressed="3.2876" xmlns="http://www.specifiedcompany.com/API" />
</VideoGames>

演示小提琴here

如您所见,子元素<Types><Platform>都在XML namespace 中进行了序列化,特别是http://www.specifiedcompany.com/API

您的反序列化失败,因为在示例XML中,这些元素不在任何命名空间中。

为什么会这样?这是因为您的所有类(包括VideoGames都应用了XmlTypeAttribute属性,该属性指定了Namespace

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.specifiedcompany.com/API")]
public partial class VideoGames
{
}

此属性的作用是指定默认情况下,该类型的所有属性都应序列化到指定的名称空间。 (与XmlRootAttribute比较,它指定如何对根元素<VideoGames>本身(而不只是其子元素)进行序列化。)

如果您不想这样做,请从c#类XmlTypeAttribute.NamespaceVideoGamesPlatformGameType中删除Code设置(演示小提琴# 2 here)。如果确实需要,请修改XML以包括所需的名称空间,如此答案中的输出XML所示。

相关问题