NULL - 反序列化XML时的值

时间:2015-07-02 13:23:30

标签: c# xml null deserialization

我尝试反序列化根据相同模式构建的不同XML文档,是的,我是这个主题的新手。我设法检索一些自动生成的代码,使用xsd文件作为“类型定义语言”(VS2013),对我来说它看起来没问题,基本上它正在工作,这意味着我可以启动程序。但是,我的程序没有输出,通过调试我可以看到最重要的字段没有填充。这是情况(原始生成的代码和XML当然要长得多,但我切入产生问题的类):

XML:

<?xml version="1.0" encoding="UTF-8"?>
<mainDatas 
  xmlns="http://www.myNamespace.com/"
  xmlns:ons="http://www.myOtherNamespace.com/"
  xmlns:aan="http://www.andAnotherNamespace.com/">
  <contains>
    <ons:FeatureCollection aan:id="UHL">
      <aan:boundedBy>999.9</aan:boundedBy>
      <aan:featureMember>
        <myClass1 aan:id="XXX0001">
        </myClass1>
      </aan:featureMember>
        <myClass2 aan:id="XXX0002">
        </myClass2>
      </aan:featureMember>
        <myClass1 aan:id="XXX0003">
        </myClass1>
      </aan:featureMember>
    </ons:FeatureCollection>
  </contains>
</mainDatas>

C#(自动生成):

[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "12.0.30723.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.myNamespace.com/")]
[System.Xml.Serialization.XmlRootAttribute("mainDatas", Namespace="http://www.myNamespace.com/", IsNullable=false)]
public partial class mainDatas
{
    private mainDatasContains containField;

    public mainDatasContains contain
    {
        get
        {
            return this.containField;
        }
        set
        {
            this.containField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "12.0.30723.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.myNamespace.com/")]
public partial class mainDatasContains
{

    private FeatureCollectionType1 featureCollectionField;

[System.Xml.Serialization.XmlElementAttribute(Namespace="http://www.myOtherNamespace.com/")]
    public FeatureCollectionType1 FeatureCollection
    {
        get
        {
            return this.featureCollectionField;
        }
        set
        {
            this.featureCollectionField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "12.0.30723.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName="FeatureCollectionType", Namespace="http://www.myOtherNamespace.de/")]
[System.Xml.Serialization.XmlRootAttribute("FeatureCollection", Namespace="http://www.myOtherNamespace.de/", IsNullable=false)]
public partial class FeatureCollectionType1
{

    private string lockIdField;

    private FeaturePropertyType[] featureMemberField;

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

    [System.Xml.Serialization.XmlElementAttribute("featureMember")]
    public FeaturePropertyType[] featureMember
    {
        get
        {
            return this.featureMemberField;
        }
        set
        {
            this.featureMemberField = value;
        }
    }       
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "12.0.30723.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.andAnotherNamespace.com/")]
[System.Xml.Serialization.XmlRootAttribute("featureMember", Namespace="http://www.andAnotherNamespace.com/", IsNullable=false)]
public partial class FeaturePropertyType
{

  private AbstractFeatureType itemField;

  private bool ownsField;

  public FeaturePropertyType()
  {
      this.ownsField = false;
  }

  [System.Xml.Serialization.XmlElementAttribute("DynamicFeature", typeof(DynamicFeatureType))]
  [System.Xml.Serialization.XmlElementAttribute("FeatureCollection", typeof(FeatureCollectionType))]
  [System.Xml.Serialization.XmlElementAttribute("Observation", typeof(ObservationType))]
  public AbstractFeatureType Item
  {
      get
      {
          return this.itemField;
      }
      set
      {
          this.itemField = value;
      }
  }

  [System.Xml.Serialization.XmlAttributeAttribute()]
  [System.ComponentModel.DefaultValueAttribute(false)]
  public bool owns
  {
      get
      {
          return this.ownsField;
      }
      set
      {
          this.ownsField = value;
      }
  }
}

[System.Xml.Serialization.XmlIncludeAttribute(typeof(myClass1))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(myClass2))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "12.0.30723.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.andAnotherNamespace.com/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.andAnotherNamespace.com/", IsNullable=true)]
public abstract partial class AbstractFeatureType
{

    private BoundingShapeType boundedByField;

    private LocationPropertyType itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public BoundingShapeType boundedBy
    {
        get
        {
            return this.boundedByField;
        }
        set
        {
            this.boundedByField = value;
        }
    }
[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "12.0.30723.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myNamespace.com")]
[System.Xml.Serialization.XmlRootAttribute("myClass1", Namespace="http://www.myNamespace.com/", IsNullable=false)]
public partial class myClass1 : AbstractFeatureType
{
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("MSBuild", "12.0.30723.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myNamespace.com")]
[System.Xml.Serialization.XmlRootAttribute("myClass2", Namespace="http://www.myNamespace.com/", IsNullable=false)]
public partial class myClass2 : AbstractFeatureType
{
}

很抱歉发布了那么多自动生成的代码。我试图尽可能地省略,但由于我很无能,我不确定什么是必要的,什么不是。

现在我的工作:

XmlSerializer serializer = new XmlSerializer(typeof(mainDatas));
XmlTextReader reader = new XmlTextReader(@".\myData.xml");
mainDatas dataObject = (mainDatas)serializer.Deserialize(reader);

FeaturePropertyType[] allObjects = dataObject.contain.FeatureCollection.featureMember;

foreach(FeaturePropertyType feature in allObjects)
{
   if (feature.Item != null && feature.Item.GetType() == typeof(myClass1))
   {
        this.doSomething((myClass1)feature.Item));
   }
   else
   {
        this.doSomethingElse();
   }
}

调试时,它显示allObjects包含3个FeaturePropertyType类型的项目(即xml-sheet中myClass1和myClass2的数量为可疑)但这些项目的属性Item为null。

当我在互联网上搜索时,许多人认为它可能是命名空间的问题。所以我尝试了一点点来回改变它但没有成功。无论如何,我不认为在自动生成的代码中进行更改是一个好主意。

如果有人能就这个问题给我一些建议,我会很高兴。

问候 TM

编辑: 我不得不编辑我的代码,因为我忘了用myClass1和myClass2编写AbstractFeatureType的继承。

1 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            MainDatas mainDatas = new MainDatas() {
                contains = new MainDatasContains() {
                    featureCollection = new FeatureCollection(){
                        id = "UHL",
                        boundedBy = 999.9,
                        featureMembers = new List<FeatureMember>() {
                            new FeatureMember() {
                                myClass1 = new MyClass1() {
                                    id = "XXX0001"
                                }
                            },
                            new FeatureMember() {
                                myClass2 = new MyClass2() {
                                    id = "XXX0001"
                                }
                            },
                            new FeatureMember() {
                                myClass1 = new MyClass1() {
                                    id = "XXX0003"
                                }
                            }

                        }
                    }
                }
            };


            XmlSerializer serializer = new XmlSerializer(typeof(MainDatas));

            StreamWriter writer = new StreamWriter(FILENAME);
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("aan", "http://www.andAnotherNamespace.com/");
            ns.Add("ons", "http://www.myOtherNamespace.com/");
            ns.Add("", "http://www.myNamespace.com/");

            serializer.Serialize(writer, mainDatas, ns);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            XmlSerializer xs = new XmlSerializer(typeof(MainDatas));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            MainDatas newMainDatas = (MainDatas)xs.Deserialize(reader);
        }
    }
    [XmlRoot("mainDatas", Namespace = "http://www.myNamespace.com/")]   
    public class MainDatas
    {

        [XmlElement("contains")]
        public MainDatasContains contains { get; set; }
    }

    [XmlRoot("contains")]
    public class MainDatasContains
    {

        [XmlElement("FeatureCollection", Namespace = "http://www.myOtherNamespace.com/")]
        public FeatureCollection featureCollection { get; set; }
    }

    [XmlRoot(ElementName = "FeatureCollection")]
    public class FeatureCollection
    {
        [XmlAttribute("id", Namespace = "http://www.andAnotherNamespace.com/")]
        public string id { get; set; }

        [XmlElement("boundedBy", Namespace = "http://www.andAnotherNamespace.com/")]
        public double boundedBy { get; set; }

        [XmlElement("featureMember", Namespace = "http://www.andAnotherNamespace.com/")]
        public List<FeatureMember> featureMembers { get; set; }
    }

    [XmlRoot("featureMember")]
    public class FeatureMember
    {
        [XmlElement("myClass1")]
        public MyClass1 myClass1 { get; set; }

        [XmlElement("myClass2")]
        public MyClass2 myClass2 { get; set; }
    }

    [XmlRoot("myClass1")]
    public class MyClass1
    {
        [XmlAttribute("id", Namespace = "http://www.andAnotherNamespace.com/")]
        public string id { get; set; }

    }

    [XmlRoot("myClass2")]
    public class MyClass2
    {
        [XmlAttribute("id")]
        public string id { get; set; }

    }
}
​