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

标签 c# xml

我正在尝试使用 C# 反序列化以下 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>

我的类(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<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?

最佳答案

好的,我已经通过更改 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>

关于c# - 反序列化 XML 时无法识别指定的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26293499/

相关文章:

java - 在Java中追加多个NodeList

c# - 如何使用 Mono 在 XSP 上运行 .NET 4.5?

c# - 使用 C# 修改子字符串

c# - List<string> -> xml反序列化

java - 使用Struts 2时如何配置web.xml?

c# - 使用 XpathNavigator 在 C# 中读取 XML 文件

c# - ASP.NET Web API 本地主机 :xxxx/api/product defaults to Home Page and not JSON data(orXML)

c# - MS 单元测试中的异常?

c# - Process.Start 不适用于 Filezilla 的 UNC 路径

c# - 如何防止 XXE 攻击(.NET 中的 XmlDocument)