gpt4 book ai didi

c# - 从带有图像的 XSD 站点地图生成 C# 类

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:12 25 4
gpt4 key购买 nike

有人可以帮我从 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; }
}
}

但我有前缀“image:”的问题,我不知道如何将这个前缀添加到序列化的 xml。如果我通过添加分号修改元素名称,那么它会转义 'image_x003A_image'

最佳答案

我也用过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;
}
}

然后将“图像:”前缀添加到我所有的图像元素。您应该能够为您的图像列表执行类似的操作,以将其与您在上面创建的命名空间相关联。

这篇文章似乎有点老了,但希望这能帮助那些在尝试使用图像站点地图架构扩展时遇到类似问题的人。

关于c# - 从带有图像的 XSD 站点地图生成 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com