2014-10-10 78 views
2

我想用C#反序列化下面的XML:指定的类型无法识别错误反序列化XML

<stix:STIX_Package xmlns:stixVocabs="http://stix.mitre.org/default_vocabularies-1" 
    xmlns:stixCommon="http://stix.mitre.org/common-1" 
    xmlns:stix="http://stix.mitre.org/stix-1" 
    xmlns:indicator="http://stix.mitre.org/Indicator-2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    id="repository:03163c66-23ed-4e7f-8814-be1d08406" version="1.0"> 
    <stix:Indicators> 
     <stix:Indicator id="repository:9df9af32-3b29-4482-81ac-9c090a44db8c" 
      xsi:type="indicator:IndicatorType" negate="false" version="2.0"> 
      <indicator:Title>admin on 24th September 2014 - (1) FileObjects</indicator:Title> 
      <indicator:Type xsi:type="stixVocabs:IndicatorTypeVocab- 1.0">Exfiltration</indicator:Type> 
      <indicator:Description>Some Ex filtration Happened</indicator:Description> 
     </stix:Indicator> 
     <stix:Indicator id="repository:9df9af32-3b29-4482-81ac-9c090a44db8d" xsi:type="indicator:IndicatorType" negate="false" version="2.0"> 
      <indicator:Title>admin on 24th September 2014 - (2) FileObjects</indicator:Title> 
      <indicator:Type xsi:type="stixVocabs:IndicatorTypeVocab-1.0">Exfiltration</indicator:Type> 
      <indicator:Description>Some Ex filtration Happened Again</indicator:Description> 
     </stix:Indicator> 
    </stix:Indicators> 
</stix:STIX_Package> 

我的阶级结构:

[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<ControlledVocabularyStringType> 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: There is an error in XML document (3, 10). ---> System.InvalidOperationException: The specified type was not recognized: name='IndicatorType', namespace=' http://stix.mitre.org/Indicator-2 ', at http://stix.mitre.org/stix-1'>."

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

+0

难道是因为您已将类IndicatorType注释为“XmlType”指示符吗? – Nattrass 2014-10-10 07:27:07

+0

@Nattrass从IndicatorType类中删除XmlType属性后,我仍然得到与上面相同的错误。 – Brantino 2014-10-10 13:36:23

+0

您可以尝试使用xsd.exe从xml创建架构,然后再次使用xsd.exe从架构中派生类 – Nattrass 2014-10-10 17:15:52

回答

5

好吧,我设法通过改变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<ControlledVocabularyStringType> 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的根元素中定义

<stix:Indicator xsi:type="indicator:IndicatorType"> <---HERE 
    <indicator:Title>admin on 24th September 2014 - (1) FileObjects</indicator:Title> 
    <indicator:Type xsi:type="stixVocabs:IndicatorTypeVocab-1.0">Exfiltration</indicator:Type> <--- HERE 
    <indicator:Description>Some Ex filtration Happened</indicator:Description> 
</stix:Indicator>