有人可以帮我从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; }
}
}
但我有前缀&#39;图片的问题:&#39; ,我无法理清如何将此前缀添加到序列化的xml中。如果我通过添加分号修改元素名称,那么执行转义&#39; image_x003A_image&#39;
答案 0 :(得分:0)
我还使用了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
[XmlElement(Namespace = "http://www.google.com/schemas/sitemap-image/1.1")]
public image image
{
get
{
return this.imageField;
}
set
{
this.imageField = value;
}
}
然后将“image:”前缀添加到我的所有图像元素中。您应该能够为您的图像列表执行类似操作,以将其与您在上面创建的命名空间相关联。
这篇文章似乎有点陈旧,但希望这有助于遇到类似头痛的人尝试使用图像站点地图架构扩展。