无法识别指定的类型错误反序列化XML

我正在尝试使用C#反序列化以下XML:

   admin on 24th September 2014 - (1) FileObjects Exfiltration Some Ex filtration Happened   admin on 24th September 2014 - (2) FileObjects Exfiltration Some Ex filtration Happened Again    

我的class级结构:

 [XmlType(AnonymousType = true, Namespace = "http://stix.mitre.org/stix-1")] [XmlRoot(Namespace = "http://stix.mitre.org/stix-1", IsNullable = false)] public class STIX_Package { [XmlArrayItemAttribute("Indicator", IsNullable = false)] public IndicatorType[] Indicators { get; set; } [XmlAttribute] public string id { get; set; } [XmlAttribute] public decimal version { get; set; } } [XmlRoot(ElementName = "Indicator")] [XmlType("Indicator", Namespace = "http://stix.mitre.org/stix-1")] public class IndicatorType : IndicatorBaseType { [XmlElement("Title", Namespace = "http://stix.mitre.org/Indicator-2")] public string Title { get; set; } [XmlElement("Type", Namespace = "http://stix.mitre.org/Indicator-2")] public List Type { get; set; } [XmlElement("Description", Namespace = "http://stix.mitre.org/Indicator-2")] public StructuredTextType Description { get; set; } [XmlAttribute, System.ComponentModel.DefaultValueAttribute(false)] public bool negate { get; set; } } [XmlRoot(ElementName = "Indicator")] [XmlInclude(typeof(IndicatorType))] public class IndicatorBaseType { [XmlAttribute] public XmlQualifiedName id { get; set; } [XmlAttribute] public string version { get; set; } } public class ControlledVocabularyStringType { public string vocab_name { get; set; } public string vocab_reference { get; set; } [XmlText] public string Value { get; set; } } 

我的反序列化代码:

 using (var stream = new StreamReader("Test.xml")) { var xml = new XmlSerializer(typeof(STIX_Package)); return (STIX_Package) xml.Deserialize(stream); } 

反序列化会产生错误:

“System.InvalidOperationException:XML文档中存在错误( 3,10 ).—> System.InvalidOperationException:无法识别指定的类型:name =’IndicatorType’,namespace =’ http://stix.mitre。 org / Indicator-2 ‘,http://stix.mitre.org/stix-1’>。“

如何构建/注释我的POCO以便上面的XML可以反序列化?

好吧,我已经设法通过改变IndicatorType类来反序列化你的xml。 我已经更改了IndicatorType类上的命名空间和Type属性上的命名空间

 [XmlRoot(ElementName = "Indicator")] [XmlType(Namespace = "http://stix.mitre.org/Indicator-2", TypeName = "IndicatorType")] public class IndicatorType : IndicatorBaseType { [XmlElement("Title", Namespace = "http://stix.mitre.org/Indicator-2")] public string Title { get; set; } [XmlElement("Type", Namespace = "http://stix.mitre.org/default_vocabularies-1")] public List Type { get; set; } [XmlElement("Description", Namespace = "http://stix.mitre.org/Indicator-2")] public string Description { get; set; } [XmlAttribute, System.ComponentModel.DefaultValueAttribute(false)] public bool negate { get; set; } } 

如果您查看XML,您可以看到元素位于不同的命名空间中。 这些是在XML的根元素中定义的

  <---HERE admin on 24th September 2014 - (1) FileObjects Exfiltration <--- HERE Some Ex filtration Happened