【问题标题】:xsd.exe ignores cdata?xsd.exe 忽略 cdata?
【发布时间】:2012-10-26 12:41:47
【问题描述】:

我使用 Microsoft SDK 中的 xsd.exe 首先生成 xml 文件的架构,然后生成先前生成的 xsd 文件的 C# 文件。 这是我的 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/>

现在生成的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>

以及生成的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;
    }
}
}

现在我的问题是:为什么 xsd.exe 会忽略 CDATA 中嵌入的 xml 代码?是否有可能强制 xsd.exe 不忽略 CDATA?或者是否可以生成 2 个 xsd 和 cs 文件并告诉第一个(其中包含事件)有一个 CDATA 节点,其中包含另一个 xml 文件并将它们链接到生成的代码中?

抱歉发了这么长的帖子,希望有人能帮助我:)

【问题讨论】:

    标签: c# xml xsd xml-serialization xml-deserialization


    【解决方案1】:

    如果使用 CDATA,解析器将忽略 CDATA 部分内的所有内容。如果您的目标是获取 CDATA 中的部分,请点击此链接。

    Getting CDATA XML Section Using Linq

    【讨论】:

      【解决方案2】:

      我使用 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>
      

      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;
              }
          }
      }
      

      并使用 XmlSerializer.Deserialize 用 CData 填充 payload.Value

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 2011-01-09
        • 1970-01-01
        • 2011-04-08
        • 1970-01-01
        • 1970-01-01
        • 2021-06-17
        相关资源
        最近更新 更多