Я новичок в XML я хочу этот тип выхода:Добавить префикс в 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
[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.cs подобно этому
[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>
так, как я могу добавить префикс изображение тега изображения ??
Великий, это лучший ответ все, что я видел. – juanytuweb