首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指定类型无法识别反序列化XML错误。

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

Stack Overflow用户
提问于 2014-10-10 06:50:50
回答 1查看 12.9K关注 0票数 8

我试图使用C#反序列化以下XML:

代码语言:javascript
复制
<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>

我的班级结构:

代码语言:javascript
复制
[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; }
}

我的反序列化代码:

代码语言:javascript
复制
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',at http://stix.mitre.org/stix-1'>.。“

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-11 18:06:41

好的,我已经通过修改IndicatorType类来反序列化您的xml。我已经更改了IndicatorType类上的名称空间和Type属性上的命名空间

代码语言:javascript
复制
[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的根元素中定义的。

代码语言:javascript
复制
<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>
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26293499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档