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

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

博客园 - 什么都不知道

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;
}