C# 站点地图图像元素
我有一个在c#中构建xml站点地图的类,我在mvc控制器中使用它来生成站点地图:C# 站点地图图像元素,c#,xml,nsxmlelement,C#,Xml,Nsxmlelement,我有一个在c#中构建xml站点地图的类,我在mvc控制器中使用它来生成站点地图: public class Location { public enum eChangeFrequency { always, hourly, daily, weekly, monthly, yearly, ne
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>
http://...
0.5
http://...
但我想要正确的格式,比如:
<url>
<loc>http://...</loc>
<priority>0.5</priority>
<image:image>
<image:loc>http://...</image:loc>
</image:image>
</url>
[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; }
}
}
http://...
0.5
http://...
我想将此前缀添加到图像元素,
感谢您的帮助阅读本页有很好的例子。
您应该在映像类中添加XmlSerializerNamespaces类型属性,并且应该添加字符串前缀和字符串命名空间值 我读了这个 用法必须如下所示
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);
我这样加上:
<url>
<loc>http://...</loc>
<priority>0.5</priority>
<image:image>
<image:loc>http://...</image:loc>
</image:image>
</url>
[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>
http://...
0.5
http://...
非常感谢,您能为此类编写此属性的正确格式吗?