简体   繁体   English

xsd.exe忽略cdata?

[英]xsd.exe ignores cdata?

I used xsd.exe from Microsoft SDKs to first generate a schema of an xml file and then generate a C# file of the previously generated xsd file. 我使用Microsoft SDK中的xsd.exe首先生成xml文件的架构,然后生成先前生成的xsd文件的C#文件。 Here is my xml file: 这是我的xml文件:

<event topic="event.system.visualization.initialization.response" subject="CIBEK204Test0" producer="de.cibek.configurator" timeToLive="0" too="1351252459919" duration="0">
<payload>
    <![CDATA[
        <?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <building>
                <floor name="">
                    <location name="Wohnen">
                        <item category="event.environment.apartment.window" groupaddress="10248" location="Wohnen" location.floor="" location.specification="Mitte links"/>
                    </location>
                </floor>
            </building>
    ]]>
</payload>
<properties/>
<traces/>

Now the generated xsd: 现在生成的xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="event">
 <xs:complexType>
  <xs:sequence>
    <xs:element name="payload" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
    <xs:element name="properties" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
    <xs:element name="traces" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
  </xs:sequence>
  <xs:attribute name="topic" type="xs:string" />
  <xs:attribute name="subject" type="xs:string" />
  <xs:attribute name="producer" type="xs:string" />
  <xs:attribute name="timeToLive" type="xs:string" />
  <xs:attribute name="too" type="xs:string" />
  <xs:attribute name="duration" type="xs:string" />
 </xs:complexType>
 </xs:element>
 <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
  <xs:complexType>
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element ref="event" />
  </xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

And the generated cs file: 并生成cs文件:

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.17929.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class @event {

private string payloadField;

private string propertiesField;

private string tracesField;

private string topicField;

private string subjectField;

private string producerField;

private string timeToLiveField;

private string tooField;

private string durationField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string payload {
    get {
        return this.payloadField;
    }
    set {
        this.payloadField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string properties {
    get {
        return this.propertiesField;
    }
    set {
        this.propertiesField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string traces {
    get {
        return this.tracesField;
    }
    set {
        this.tracesField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string topic {
    get {
        return this.topicField;
    }
    set {
        this.topicField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string subject {
    get {
        return this.subjectField;
    }
    set {
        this.subjectField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string producer {
    get {
        return this.producerField;
    }
    set {
        this.producerField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string timeToLive {
    get {
        return this.timeToLiveField;
    }
    set {
        this.timeToLiveField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string too {
    get {
        return this.tooField;
    }
    set {
        this.tooField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string duration {
    get {
        return this.durationField;
    }
    set {
        this.durationField = value;
    }
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class NewDataSet {

private @event[] itemsField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("event")]
public @event[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}
}

Now my question is: Why does xsd.exe ignore the embedded xml code in CDATA? 现在我的问题是:为什么xsd.exe忽略CDATA中的嵌入式xml代码? Is there any possibility to force xsd.exe to NOT ignore CDATA? 是否有可能迫使xsd.exe不忽略CDATA? Or is it possible to generate 2 xsd and cs files and tell the first one (with event in it) that there is a CDATA node that has another xml file in it and link them in the generated code? 还是可以生成2个xsd和cs文件,并告诉第一个文件(其中包含事件),其中有一个CDATA节点,其中包含另一个xml文件,并将它们链接到生成的代码中?

Sorry for the long post i hope anyone can help me :) 对不起,很长的帖子,我希望任何人都能帮助我

If CDATA is used, the parser ignores everything inside the CDATA section. 如果使用CDATA,则解析器将忽略CDATA节中的所有内容。 If your aim is to get the section inside CDATA, please follow this link. 如果您的目标是在CDATA中获得该部分,请单击此链接。

Getting CDATA XML Section Using Linq 使用Linq获取CDATA XML部分

I successfully deserialize CDATA by using xsd.exe and the following xsd file : 我通过使用xsd.exe和以下xsd文件成功反序列化了CDATA:

<xs:element name="event">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="payload" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="payload">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string"/>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

The generated code by xsd.exe is : xsd.exe生成的代码是:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class @event {

    private payload[] payloadField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("payload")]
    public payload[] payload {
        get {
            return this.payloadField;
        }
        set {
            this.payloadField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class payload {

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

And using XmlSerializer.Deserialize fill the payload.Value with the CData 并使用XmlSerializer.Deserialize用CData填充payload.Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM