在 xml 序列化对象中添加前缀

Add prefix in xml serialize object

我是 XML 的新手,我想要这种类型的输出:

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <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>

为此我创建了 Sitemap.cs class

[Serializable]
[XmlRoot(ElementName = "urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public class Sitemap
{
    public Sitemap()
    {
        Urls = new List<SitemapUrl>();
    }

    [XmlElement(ElementName = "url")]
    public List<SitemapUrl> Urls { get; set; }
}

我的Sitemapurl.csclass是这样的

[Serializable]
public class SitemapUrl
{
    [XmlElement(ElementName = "loc")]
    public string UrlLocation { get; set; }

    [XmlElement(ElementName = "lastmod")]
    public string LastModificationDate { get; set; }

    [XmlElement(ElementName = "changefreq")]
    public string ChangingFrequency { get; set; }

    [XmlElement(ElementName = "priority")]
    public float Priority { get; set; }

    [XmlElement(ElementName = "image")]
    public List<Image> ImageList { get; set; }
}


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

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

    //optional
    [XmlElement(ElementName = "geo_location")]
    public string GeoLocation { get; set; }

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

    //optional
    [XmlElement(ElementName = "license")]
    public string License { get; set; }
}

我创建 xml 并在 xml 中添加数据的代码是

var path = HttpContext.Current.Server.MapPath("sitemap.xml");
                XmlTextReader textReader = new XmlTextReader(path);
                System.Xml.Serialization.XmlSerializer writer =
                 new System.Xml.Serialization.XmlSerializer(typeof(Sitemap));
                XmlSerializerNamespaces nameSpace = new XmlSerializerNamespaces();
                nameSpace.Add("image", "http://www.google.com/schemas/sitemap-image/1.1");

                nameSpace.Add("video", "http://www.google.com/schemas/sitemap-video/1.1");

                System.IO.StreamWriter file = new System.IO.StreamWriter(path);


                writer.Serialize(file, sitemap, nameSpace);


                file.Close();

使用这个我得到了这种类型的输出

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

    <image>
      <loc>http://example.com/image.jpg/</loc>

    </image>
  </url>

那么如何在图像标签中添加前缀图像??

"image"前缀对应的命名空间添加到ImageList附加的XmlElementAttribute.Namespace属性属性:

    [XmlElement(ElementName = "image", Namespace = "http://www.google.com/schemas/sitemap-image/1.1")]
    public List<Image> ImageList { get; set; }

这样做会设置分配给序列化 class 时产生的 XML 元素的命名空间。由于您在序列化期间将命名空间 "http://www.google.com/schemas/sitemap-image/1.1" 映射到 "image:" 前缀,因此将出现此内容。

如需进一步阅读,请参阅 Controlling XML Serialization Using Attributes