簡體   English   中英

Sitemap圖片元素

[英]Sitemap image element

我在C#中有一個用於構建xml網站地圖的類,我在mvc控制器中使用它來生成網站地圖:

public class Location
    {
        public enum eChangeFrequency
        {
            always,
            hourly,
            daily,
            weekly,
            monthly,
            yearly,
            never
        }

        [XmlElement("loc")]
        public string Url { get; set; }

        [XmlElement("changefreq")]
        public eChangeFrequency? ChangeFrequency { get; set; }
        public bool ShouldSerializeChangeFrequency() { return ChangeFrequency.HasValue; }

        [XmlElement("lastmod")]
        public DateTime? LastModified { get; set; }
        public bool ShouldSerializeLastModified() { return LastModified.HasValue; }

        [XmlElement("priority")]
        public double? Priority { get; set; }
        public bool ShouldSerializePriority() { return Priority.HasValue; }

        [XmlElement("image")]
        public Image Image { get; set; }
    }

    [XmlType("image")]
    public class Image
    {
        [XmlElement(ElementName = "loc")]
        public string UrlLocation { get; set; }

        [XmlElement(ElementName = "caption")]
        public string Caption { get; set; }

        [XmlElement(ElementName = "title")]
        public string Title { get; set; }

    }

這是輸出,當我使用它時:

<url>
  <loc>http://...</loc>
  <priority>0.5</priority>
    <image>
      <loc>http://...</loc>
    </image>
</url>

但是我想要正確的格式,像這樣:

<url>
  <loc>http://...</loc>
  <priority>0.5</priority>
  <image:image>
    <image:loc>http://...</image:loc>
  </image:image>
</url>

我想將此前綴添加到圖像元素,謝謝您的幫助

閱讀此頁面https://msdn.microsoft.com/tr-tr/library/system.xml.serialization.xmlnamespacedeclarationsattribute(v=vs.110).aspx
有一個很好的例子。
您應該在Image類中添加XmlSerializerNamespaces type屬性,並且應該添加字符串前綴和字符串名稱空間值。

我閱讀了此XML序列化和名稱空間前綴

[XmlType("image", Namespace = "http://flibble")]
public class Image
{
    [XmlElement(ElementName = "loc")]
    public string UrlLocation { get; set; }

    [XmlElement(ElementName = "caption")]        
    public string Caption { get; set; }

    [XmlElement(ElementName = "title")]
    public string Title { get; set; }

}  

用法必須如下。

Image mySelect = new Image();
        mySelect.Caption = "Caption";
        mySelect.Title = "Title";
        mySelect.UrlLocation = "www";

        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("image", "http://flibble");
        XmlSerializer ser = new XmlSerializer(typeof(Image));

        ser.Serialize(Console.Out, mySelect,ns);

我這樣添加它:

[XmlType("image")]
    public class Image
    {
        [XmlElement(ElementName = "loc")]
        public string UrlLocation { get; set; }

        [XmlElement(ElementName = "caption")]
        public string Caption { get; set; }

        [XmlElement(ElementName = "title")]
        public string Title { get; set; }

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces NameSpace
        {
            get
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("image", "http://www.google.com/schemas/sitemap-image/1.1");
                return ns;
            }
            set { NameSpace = value; }
        }

    }

這是輸出:

<url>
  <loc>http://...</loc>
  <priority>0.5</priority>
    <image xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
      <loc>http://...</loc>
    </image>
</url>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM