如何使用XmlSerializer在c#中序列化google sitemap
我想像这样序列化如何使用XmlSerializer在c#中序列化google sitemap,c#,xml-serialization,xml-sitemap,C#,Xml Serialization,Xml Sitemap,我想像这样序列化 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"> </urlset> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http:/
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
</urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>http://www.example.org/business/article55.html</loc>
<news:news></news:news>
</url>
<url>
<loc>http://www.example.org/business/page1.html</loc>
<lastmod>2010-10-10</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
结果就在这里
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:d1p1="xmlns" d1p1:news="http://www.google.com/schemas/sitemap-news/0.9"/>
我的班级声明有什么问题吗?
另一个问题2
我怎么能这样申报
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
</urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
<url>
<loc>http://www.example.org/business/article55.html</loc>
<news:news></news:news>
</url>
<url>
<loc>http://www.example.org/business/page1.html</loc>
<lastmod>2010-10-10</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
http://www.example.org/business/article55.html
http://www.example.org/business/page1.html
2010-10-10
每周的
我的声明在这里
[XmlRoot("urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public class GoogleSiteMap
{
public GoogleSiteMap()
{
Urls = new List<gUrlBase>();
}
//[XmlElement("url")]
[XmlElement("url",Type = typeof(gNormalUrl))]
[XmlElement("url",Type = typeof(gNewsUrl))]
public List<gUrlBase> Urls { get; set; }
}
[XmlRoot(“urlset”,名称空间=”http://www.sitemaps.org/schemas/sitemap/0.9")]
公共类谷歌网站地图
{
谷歌公共网站地图()
{
URL=新列表();
}
//[XmlElement(“url”)]
[XmlElement(“url”,Type=typeof(gNormalUrl))]
[XmlElement(“url”,Type=typeof(gNewsUrl))]
公共列表URL{get;set;}
}
这是返回错误
命名空间中的XML元素“url”http://www.sitemaps.org/schemas/sitemap/0.9'已存在于当前范围中。使用XML属性为元素指定另一个XML名称或命名空间。
如何声明相同的根名称“url”?您需要使用正确的名称空间:
[XmlRoot("urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
您可以删除xmlns
和xmlnsNews
属性-它们做其他事情。同样,需要将“”中的任何数据标记为这样的名称空间。名称空间别名是否为news
无关紧要(它只是一个别名),但如果愿意,可以通过XmlSerializerNamespaces
进行控制。您不需要[Serializable]
例如,如果每个
都需要位于“”命名空间中,并且您希望将“”用作整个命名空间,并将“”别名为“news”,则:
静态类程序
{
静态void Main()
{
var ns=新的XmlSerializerNamespaces();
添加(“,”http://www.sitemaps.org/schemas/sitemap/0.9");
添加(“新闻”http://www.google.com/schemas/sitemap-news/0.9");
var ser=新的XmlSerializer(typeof(GoogleSiteMap));
var obj=新谷歌网站地图{url=新列表{“abc”、“def”、“ghi”};
序列序列化(Console.Out、obj、ns);
}
}
[XmlRoot(“urlset”,名称空间=”http://www.sitemaps.org/schemas/sitemap/0.9")]
公共类谷歌网站地图
{
[XmlElement(“url”,命名空间=”http://www.google.com/schemas/sitemap-news/0.9")]
公共列表URL{get;set;}
}
这将产生:
<urlset
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<news:url>abc</news:url>
<news:url>def</news:url>
<news:url>ghi</news:url>
</urlset>
<urlset xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.org/business/article55.html</loc>
<news:news />
</url>
<url>
<loc>http://www.example.org/business/page1.html</loc>
<lastmod>2010-10-10T00:00:00</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
abc
def
ghi
(我没有检查实际的内容名称空间是什么-这只是为了显示数据中的名称空间、xml中的名称空间和名称空间别名之间的关系)
重新编辑-类似于:
static class Program
{
static void Main()
{
var ns = new XmlSerializerNamespaces();
ns.Add("", "http://www.sitemaps.org/schemas/sitemap/0.9");
ns.Add("news", "http://www.google.com/schemas/sitemap-news/0.9");
var ser = new XmlSerializer(typeof (GoogleSiteMap));
var obj = new GoogleSiteMap {Urls = {
new SiteUrl { Location = "http://www.example.org/business/article55.html", News = ""},
new SiteUrl { Location = "http://www.example.org/business/page1.html", LastModified = new DateTime(2010,10,10),
ChangeFrequency = "weekly"}
}};
ser.Serialize(Console.Out, obj, ns);
}
}
[XmlRoot("urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public class GoogleSiteMap
{
private readonly List<SiteUrl> urls = new List<SiteUrl>();
[XmlElement("url")]
public List<SiteUrl> Urls { get { return urls; } }
}
public class SiteUrl
{
[XmlElement("loc")]
public string Location { get; set; }
[XmlElement("news", Namespace = "http://www.google.com/schemas/sitemap-news/0.9")]
public string News { get; set; }
[XmlElement("lastmod")]
public DateTime? LastModified { get; set; }
[XmlElement("changefreq")]
public string ChangeFrequency { get; set; }
public bool ShouldSerializeLastModified() { return LastModified.HasValue; }
}
静态类程序
{
静态void Main()
{
var ns=新的XmlSerializerNamespaces();
添加(“,”http://www.sitemaps.org/schemas/sitemap/0.9");
添加(“新闻”http://www.google.com/schemas/sitemap-news/0.9");
var ser=新的XmlSerializer(typeof(GoogleSiteMap));
var obj=新谷歌网站地图{url={
新站点URL{Location=”http://www.example.org/business/article55.html“,News=”“},
新站点URL{Location=”http://www.example.org/business/page1.html,LastModified=新日期时间(2010,10,10),
ChangeFrequency=“每周”}
}};
序列序列化(Console.Out、obj、ns);
}
}
[XmlRoot(“urlset”,名称空间=”http://www.sitemaps.org/schemas/sitemap/0.9")]
公共类谷歌网站地图
{
私有只读列表URL=新列表();
[XmlElement(“url”)]
公共列表URL{get{return url;}}
}
公共类站点URL
{
[XmlElement(“loc”)]
公共字符串位置{get;set;}
[XmlElement(“新闻”,命名空间=”http://www.google.com/schemas/sitemap-news/0.9")]
公共字符串新闻{get;set;}
[XmlElement(“lastmod”)]
公共日期时间?LastModified{get;set;}
[XmlElement(“changefreq”)]
公共字符串更改频率{get;set;}
public bool应序列化LastModified(){return LastModified.HasValue;}
}
由此产生:
<urlset
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<news:url>abc</news:url>
<news:url>def</news:url>
<news:url>ghi</news:url>
</urlset>
<urlset xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.example.org/business/article55.html</loc>
<news:news />
</url>
<url>
<loc>http://www.example.org/business/page1.html</loc>
<lastmod>2010-10-10T00:00:00</lastmod>
<changefreq>weekly</changefreq>
</url>
</urlset>
http://www.example.org/business/article55.html
http://www.example.org/business/page1.html
2010-10-10T00:00:00
每周的
请使用我制作的完整代码
请参阅下面的完整代码
#region GoogleNewsSiteMap Class
[XmlRoot("urlset", Namespace = "http://www.sitemaps.org/schemas/sitemap/0.9")]
public class GoogleNewsSiteMap
{
const string _newsSiteMapSchema = "http://www.google.com/schemas/sitemap-news/0.9";
const string _newsSiteMapPrefix = "n";
public void Create(string loc, string prioity, string language, string name, string genres, string publicationDate, string title)
{
NewsSiteMap news = new NewsSiteMap();
news.Loc = loc;
news.Priority = prioity;
news.NewsSiteMapNews.Publication.Language = language;
news.NewsSiteMapNews.Publication.Name = name;
news.NewsSiteMapNews.Genres = genres;
news.NewsSiteMapNews.PublicationDate = publicationDate;
news.NewsSiteMapNews.Title = title;
List.Add(news);
}
public string GetXMLString()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
settings.Encoding = new UTF8Encoding(false);
using (StringWriter str = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(str, settings))
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(_newsSiteMapPrefix, _newsSiteMapSchema);
XmlSerializer xser = new XmlSerializer(typeof(GoogleNewsSiteMap));
xser.Serialize(writer, this, ns);
return str.ToString();
}
}
private List<NewsSiteMap> _list = null;
[XmlElement("url")]
public List<NewsSiteMap> List
{
get
{
if (_list == null)
{
_list = new List<NewsSiteMap>();
}
return _list;
}
}
#region NewsSiteMap Class
public class NewsSiteMap
{
private string _loc = string.Empty;
private string _priority = string.Empty;
private NewsSiteMap_News _newsSiteMap_News = null;
[XmlElement("loc")]
public string Loc
{
get { return _loc; } set { _loc = value; }
}
[XmlElement("priority")]
public string Priority
{
get { return _priority; } set { _priority = value; }
}
[XmlElement("news", Namespace = _newsSiteMapSchema)]
public NewsSiteMap_News NewsSiteMapNews
{
get {
if (_newsSiteMap_News == null)
{
_newsSiteMap_News = new NewsSiteMap_News();
}
return _newsSiteMap_News;
}
set { _newsSiteMap_News = value; }
}
#region NewsSiteMap_News Class
public class NewsSiteMap_News
{
private NewsSiteMap_Publication _publication = null;
private string _genres = string.Empty;
private string _publicationDate = string.Empty;
private string _title = string.Empty;
private string _keywords = string.Empty;
private string _stockTickers = string.Empty;
[XmlElement("publication", Namespace = _newsSiteMapSchema)]
public NewsSiteMap_Publication Publication
{
get
{
if (_publication == null)
{
_publication = new NewsSiteMap_Publication();
}
return _publication;
}
set { _publication = value; }
}
[XmlElement("genres")]
public string Genres
{
get { return _genres; }
set { _genres = value; }
}
[XmlElement("publication_date")]
public string PublicationDate
{
get
{
try
{
return string.Format("{0:s}", Convert.ToDateTime(_publicationDate)) + string.Format("{0:zzz}", Convert.ToDateTime(_publicationDate));
}
catch (Exception ex)
{
return _publicationDate;
}
}
set { _publicationDate = value; }
}
public string Title
{
set { _title = value; }
}
[XmlElement("title")]
public XmlCDataSection CTitle
{
get
{
XmlDocument doc = new XmlDocument();
return doc.CreateCDataSection(_title);
}
set { _title = value.Value; }
}
[XmlElement("keywords")]
public string Keywords
{
get { return _keywords; } set { _keywords = value; }
}
[XmlElement("stock_tickers")]
public string StockTickers
{
get { return _stockTickers; } set { _stockTickers = value; }
}
#region NewsSiteMap_Publication Class
public class NewsSiteMap_Publication
{
private string _name = string.Empty;
private string _language = string.Empty;
[XmlElement("name")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[XmlElement("language")]
public string Language
{
get { return _language; }
set { _language = value; }
}
}
#endregion NewsSiteMap_Publication Class
}
#endregion NewsSiteMap_News Class
}
#endregion NewsSiteMap Class
}
#endregion GoogleNewsSiteMap Class
谢谢你的回答。但我还有一个问题。请看我的问题下方的编辑:你不能让
gNormalUrl
和gNewsUrl
在同一名称空间中调用同一个东西,因为它无法确定在反序列化过程中创建哪个。这基本上是不允许的。或者使用不同的nt名称或单个具体类型。我将尝试显示一个行为符合您要求的类…2分钟。。。