惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy International News Feed
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
小众软件
小众软件
H
Hacker News: Front Page
S
Securelist
S
SegmentFault 最新的问题
Jina AI
Jina AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
P
Palo Alto Networks Blog
博客园 - 司徒正美
量子位
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
Cyberwarzone
Cyberwarzone
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
爱范儿
爱范儿
T
Threat Research - Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
T
The Exploit Database - CXSecurity.com
T
Tenable Blog
L
LINUX DO - 热门话题
MyScale Blog
MyScale Blog
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
腾讯CDC
NISL@THU
NISL@THU
A
Arctic Wolf
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
L
LINUX DO - 最新话题
The GitHub Blog
The GitHub Blog
Help Net Security
Help Net Security
C
Check Point Blog
O
OpenAI News
D
DataBreaches.Net
I
InfoQ
N
News and Events Feed by Topic
S
Security @ Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs

博客园 - 什么都不知道

VB.NET的一个小问题 Access is denied的问题 - 什么都不知道 存储过程output参数问题 SQL Server的效率? datagrid刷新问题 使用Visio DrawingControl的应用开发(补) 突起效果的Label WinForm下TextBox的数据绑定和更新 使用Radio按钮选择DataGrid行 如何在运行时加载不处于应用程序目录下的assembly 使用VSA给程序加上脚本支持 删除所有Windows组件 在ASP.NET中嵌入wml标记 c#中动态装载dll 处理大型xml文件 RedirectToMobilePage的问题 使用Visio 2003 Drawing Control开发应用(3)(4) 使用Visio 2003 Drawing Control开发应用(2) 使用Visio 2003 Drawing Control开发应用(1)
DataSet的Xml序列化问题
什么都不知道 · 2005-07-22 · via 博客园 - 什么都不知道

MSDN中指出,DataSet序列化是要调用WriteXml产生。但是当我们的一个类中包含有一个类型为DataSet的属性时,直接使用XmlSerializer来做Serialize产生的XML文件中,DataSet是必然带有schema信息的。这样是无可厚非的,如果不这样是无法反序列化的。

可是我们也许有时需要DataSet生成的XML只包括数据,并不关系结构。这样就需要写一个DataSet派生类,同时为了实现XML序列化,需要实现IXmlSerializable接口   class MyDataSet : DataSet, IXmlSerializable
除了默认的构造函数,我们需要一个通过DataSet构造MyDataSet的构造函数

public MyDataSet(DataSet inputDs)
{
 
this.DataSetName = inputDs.DataSetName;
 
this.Prefix = inputDs.Prefix;
 
this.Namespace = inputDs.Namespace;
 
this.Locale = inputDs.Locale;
 
this.CaseSensitive = inputDs.CaseSensitive;
 
this.EnforceConstraints = inputDs.EnforceConstraints;for(int index = 0; index < inputDs.Tables.Count; index++)
  
this.Tables.Add(new DataTable(inputDs.Tables[index].TableName));this.Merge(inputDs, false, MissingSchemaAction.Add);
}

可以通过实现的IXmlSerializable接口函数WriteXml写出不带架构的XML

public new void  WriteXml(XmlWriter writer)
{
 
this.WriteXml(writer, XmlWriteMode.IgnoreSchema);
}
public System.Xml.Schema.XmlSchema GetSchema()
{
 
return null;
}

另,如果还需要反序列化,那么还要实现ReadXml,这样就可以把xml反读回到DataSet中,不过原DataSet的很多结构信息就丢掉了。

public new void  ReadXml(XmlReader reader)
{
 XmlTextReader tr 
= reader as XmlTextReader;
 
bool flag = true;
 
bool hasTables = false;
 
if(tr != null)
 {
  flag 
= tr.Normalization;
  tr.Normalization 
= false;
  
this.DataSetName = tr.Name;
  hasTables 
= tr.Read();
 }
 
if(hasTables)
  
this.ReadXml(tr,XmlReadMode.Auto);
 
if(tr != null)
  tr.Normalization 
= flag;
}