繁体   English   中英

使用图像从XSD站点地图生成C#类

[英]Generate C# class from XSD sitemap with images

有人可以帮我从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; }
        }
    }

但是我对前缀'image:'问题 ,我无法弄清楚如何将此前缀添加到序列化xml中。 如果我通过添加分号来修改元素名称,则它会转义'image_x003A_image'

我还使用了xsd.exe工具,并遇到了同样的问题。 我解决如下:

urlset类中,添加以下[XmlNamespaceDeclarations]属性,并创建一个添加“ image” xml名称空间的构造函数:

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


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

就我而言,我只需要一个图像,所以我在tUrl以下属性添加到了我的image属性中

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

然后,将“ image:”前缀添加到我的所有图像元素中。 您应该能够对图像列表执行类似的操作,以将其与上面创建的名称空间相关联。

这篇文章似乎有些陈旧,但希望这可以帮助遇到类似难题的人尝试利用图像站点地图架构扩展。

暂无
暂无

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

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