相关疑难解决方法(0)

自动创建的用于xml反序列化的C#类不起作用

我正在努力为这个xml创建反序列化类:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response">
    <SOAP-ENV:Body>
        <Response>
            <Records xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:Record[1]">
                <item xsi:type="stn:Record">
                    <person xsi:type="xsd:string">John Johnos</person>
                    <address xsi:type="xsd:string">Some Street 1</address>
                    <age xsi:type="xsd:string">24</age>
                </item>
            </Records>
            <status xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:status[1]">
                <item xsi:type="stn:status">
                    <status xsi:type="xsd:string">success</status>
                    <message xsi:type="xsd:string"/>
                </item>
            </status>
        </Response>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

我试图使用自动创建的代码(在VisualStudio 12中:编辑 - >选择性粘贴 - >将XML粘贴为类):

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{

    private EnvelopeBody bodyField;

    private string encodingStyleField;

    /// <remarks/>
    public EnvelopeBody …
Run Code Online (Sandbox Code Playgroud)

c# xml xml-namespaces deserialization

15
推荐指数
2
解决办法
2617
查看次数

无法识别指定的类型错误反序列化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>
Run Code Online (Sandbox Code Playgroud)

我的班级结构:

[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; …
Run Code Online (Sandbox Code Playgroud)

c# xml

4
推荐指数
1
解决办法
7526
查看次数

xsi:type属性搞乱了C#XML反序列化

我使用XSD.exe根据XML模式(.xsd文件)自动生成C#对象.我正在反序列化OpenCover输出,但其中一个部分类没有正确生成.

这是导致异常的行:

<MethodPoint xsi:type="SequencePoint" vc="0" uspid="1" ordinal="0" offset="0" sl="19" sc="9" el="19" ec="10" bec="0" bev="0" fileid="1" />
Run Code Online (Sandbox Code Playgroud)

这是MethodPoint类的缩短版本:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class CoverageSessionModulesModuleClassesClassMethodsMethodMethodPoint {
    private string vcField;
    private string uspidField;
    private string ordinalField;
    private string offsetField;
    private string slField;
    private string scField;
    private string elField;
    private string ecField;
    private string becField;
    private string bevField;
    private string fileidField;
}
Run Code Online (Sandbox Code Playgroud)

现在我已经浏览了很多.xml文件,但OpenCover输出文件是唯一一个在属性中包含冒号的文件.MethodPoint对象也是唯一包含属性冒号的对象.如您所见,该类不包含该xsi:type属性,我知道只是添加它将因冒号而无效.你如何处理xsi前缀?

这是从其中一个OpenCover XML文件生成的原始.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="CoverageSession" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Summary">
    <xs:complexType> …
Run Code Online (Sandbox Code Playgroud)

c# xml xsd deserialization opencover

3
推荐指数
1
解决办法
2551
查看次数

标签 统计

c# ×3

xml ×3

deserialization ×2

opencover ×1

xml-namespaces ×1

xsd ×1