C#中如何反序列化使用替代组的XML?

苏丹-陈 2008-04-29 11:27:38
定义了一个抽象元素:
<element name="Filter" abstract="true">
定义了几个子类元素:
<element name="AndFilter" substitutionGroup="Filter">
<element name="OrFilter" substitutionGroup="Filter">
定义了一个容器元素:
<element name="Filters"> ... <element ref="Filter" maxOccurs="unbounded" /> ...
不知如何反序列化?
往高手指点,谢谢!
...全文
107 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏丹-陈 2008-04-29
  • 打赏
  • 举报
回复
问题解决,处理方法如下(只能枚举所有可能出现的情况了):
(1)定义一个枚举类型
public enum ItemChoiceType{
AndFilter,
OrFilter
}

(2) 添加一个枚举类型数组
public ItemChoiceType[] ItemTypes;

(3) 添加一个属性Items(属性名任意)
[XmlChoiceIdentifier("ItemTypes")]
[XmlElement("AndFilter",typeof(AndFilter))]
[XmlElement("OrFilter",typeof(OrFilter))]
public object[] Items{
get{return _items;}set{ _items = value;}
}

问题的关键是知道使用XmlChoiceIdentifier属性定义,参考.NET的帮助即可。
谢谢大家。
yilanwuyu123 2008-04-29
  • 打赏
  • 举报
回复
Mark
zhu_gx 2008-04-29
  • 打赏
  • 举报
回复
关注
zhu_gx 2008-04-29
  • 打赏
  • 举报
回复
帮你顶
cnjimbo 2008-04-29
  • 打赏
  • 举报
回复
<?xml version="1.0"?>
<Gather_Article xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>xxxxx </Title>
<UrlSource>http://www.shenhua.com/html/yaowenkuaixun/yaowenkuaixun/20080423/5378.html</UrlSource>
<Resource>国华投资公司 </Resource>
<Content>

</Content>
<IsSave>0</IsSave>
<Picture>
<string>http://www.shenhua.com/upimg/userup/0804/231020432o6.jpg</string>
</Picture>
<Author>李少宝 </Author>
<Date>2008-04-23</Date>
</Gather_Article>

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Web;
using amao.Config;
using System.Web.Caching;

namespace Entity
{
/// <summary>
/// 实体类Gather_Article 。(属性说明自动提取数据库字段的描述信息)
/// </summary>
///
[Serializable()]
public class Gather_Article
{
public Gather_Article()
{
}

#region Model
/// <summary>
/// 文章标题
/// </summary>
public string Title
{
set;
get;
}
public string UrlSource
{
get;
set;
}
/// <summary>
/// 文章说明、来源
/// </summary>
public string Resource
{
set;
get;
}
/// <summary>
/// 文章正文
/// </summary>
public string Content
{
set;
get;
}
/// <summary>
/// 是否保存
/// </summary>
public int IsSave
{
set;
get;
}
/// <summary>
/// 图片链接地址
/// </summary>
public List<string> Picture
{
set;
get;
}

public DateTime IssueDate
{
get
{
DateTime dt;
if (!DateTime.TryParse(Date, out dt))
dt=new DateTime(1999, 1, 1, 0, 0, 0);
return dt;
}
}
public string Author
{
get;
set;
}
/// <summary>
/// 发布时间
/// </summary>
public string Date
{
get;
set;
}
public bool Save()
{
try
{
string filepath=Utils.FileBasePath+"/"+this.Title.Trim().Replace("/", "").Replace("\\", "")+DateTime.Now.ToString("_MMddHHmmss")+".XML";
XmlSerializer xs=new XmlSerializer(typeof(Gather_Article));
FileInfo f=new FileInfo(filepath);
if (!f.Directory.Exists)
f.Directory.Create();
Stream stream=new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read);
xs.Serialize(stream, this);
stream.Close();
return true;
}
catch (System.Exception ex)
{
amao.Chart.Log.WriteLog(ex.ToString());
return false;
}
}
public static Gather_Article Read(string filepath)
{
if (HttpRuntime.Cache[filepath]==null)
{
Gather_Article gobj=new Gather_Article();
if (File.Exists(filepath))
{
XmlSerializer xs=new XmlSerializer(typeof(Gather_Article));
Stream stream=new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
gobj=xs.Deserialize(stream) as Gather_Article;
stream.Close();
}
else
{
return null;
}
HttpRuntime.Cache.Insert(
filepath,
gobj,
new CacheDependency(filepath),
System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromDays(1)
);
return gobj;
}
else
{
return HttpRuntime.Cache[filepath] as Gather_Article;
}
}/* */
#endregion Model

}
}

供参考,有问题给留言

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧