2014-10-28 41 views
2

有人可以幫我從XSD生成C#類(站點地圖與圖像)?從XSD站點地圖生成C#類圖像

<?xml version="1.0" encoding="UTF-8"?> 
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
    xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> 
<url> 
    <loc>http://example.com/sample.html</loc> 
    <image:image> 
    <image:loc>http://example.com/image.jpg</image:loc> 
    </image:image> 
    <image:image> 
    <image:loc>http://example.com/photo.jpg</image:loc> 
    </image:image> 
</url> 
</urlset> 

下面是xsd.exe工具生成的我的課:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, 
     Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9", 
     IsNullable = false)] 
    public partial class urlset 
    { 

     private System.Xml.XmlElement[] anyField; 

     private List<tUrl> urlField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAnyElementAttribute()] 
     public System.Xml.XmlElement[] Any 
     { 
      get { return this.anyField; } 
      set { this.anyField = value; } 
     } 

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

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")] 
    public partial class tUrl 
    { 

     private string locField; 

     private string lastmodField; 

     private tChangeFreq changefreqField; 

     private bool changefreqFieldSpecified; 

     private decimal priorityField; 

     private bool priorityFieldSpecified; 

     private System.Xml.XmlElement[] anyField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(DataType = "anyURI")] 
     public string loc 
     { 
      get { return this.locField; } 
      set { this.locField = value; } 
     } 

     /// <remarks/> 
     public string lastmod 
     { 
      get { return this.lastmodField; } 
      set { this.lastmodField = value; } 
     } 

     /// <remarks/> 
     public tChangeFreq changefreq 
     { 
      get { return this.changefreqField; } 
      set { this.changefreqField = value; } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlIgnoreAttribute()] 
     public bool changefreqSpecified 
     { 
      get { return this.changefreqFieldSpecified; } 
      set { this.changefreqFieldSpecified = value; } 
     } 

     /// <remarks/> 
     public decimal priority 
     { 
      get { return this.priorityField; } 
      set { this.priorityField = value; } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlIgnoreAttribute()] 
     public bool prioritySpecified 
     { 
      get { return this.priorityFieldSpecified; } 
      set { this.priorityFieldSpecified = value; } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlAnyElementAttribute()] 
     public System.Xml.XmlElement[] Any 
     { 
      get { return this.anyField; } 
      set { this.anyField = value; } 
     } 

     [System.Xml.Serialization.XmlElementAttribute(ElementName = "image", Type = typeof(image))] 
     public List<image> Images { get; set; } 

    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
    [System.SerializableAttribute()] 
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")] 
    public enum tChangeFreq 
    { 

     /// <remarks/> 
     always, 

     /// <remarks/> 
     hourly, 

     /// <remarks/> 
     daily, 

     /// <remarks/> 
     weekly, 

     /// <remarks/> 
     monthly, 

     /// <remarks/> 
     yearly, 

     /// <remarks/> 
     never, 
    } 

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, 
     Namespace = "http://www.google.com/schemas/sitemap-image/1.1")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.google.com/schemas/sitemap-image/1.1", 
     IsNullable = false)] 
    public partial class image 
    { 

     private string locField; 

     private string captionField; 

     private string geo_locationField; 

     private string titleField; 

     private string licenseField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(ElementName = "loc", DataType = "anyURI")] 
     public string loc 
     { 
      get { return this.locField; } 
      set { this.locField = value; } 
     } 

     /// <remarks/> 
     public string caption 
     { 
      get { return this.captionField; } 
      set { this.captionField = value; } 
     } 

     /// <remarks/> 
     public string geo_location 
     { 
      get { return this.geo_locationField; } 
      set { this.geo_locationField = value; } 
     } 

     /// <remarks/> 
     public string title 
     { 
      get { return this.titleField; } 
      set { this.titleField = value; } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute(DataType = "anyURI")] 
     public string license 
     { 
      get { return this.licenseField; } 
      set { this.licenseField = value; } 
     } 
    } 

但我有問題前綴「形象:」,我無法理清如何添加這個前綴用於序列化xml。如果我通過添加分號來修改元素名稱,那麼它做轉義'image_x003A_image'

回答

0

我也使用了xsd.exe工具,並有同樣的問題。我解決它,如下所示:

urlset類,添加以下[XmlNamespaceDeclarations]財產,並創建一個構造函數,增加了「形象」的XML命名空間:

[XmlNamespaceDeclarations] 
    public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces(); 


    public urlset() 
    { 
     xmlns.Add("image", "http://www.google.com/schemas/sitemap-image/1.1"); 
    } 

在我來說,我只需要一個單一的形象,所以我增加了以下屬性我的形象財產tUrl

 [XmlElement(Namespace = "http://www.google.com/schemas/sitemap-image/1.1")] 
    public image image 
    { 
     get 
     { 
      return this.imageField; 
     } 
     set 
     { 
      this.imageField = value; 
     } 
    } 

這就增加了「Image:」開頭我所有的圖像元素。您應該可以爲圖像列表執行類似的操作,將其與您在上面創建的命名空間相關聯。

這篇文章似乎有點舊,但希望這可以幫助有人遇到類似的問題,試圖利用圖像站點地圖架構擴展。