xml与DataSet的互转换类

简介: 以前在博客上发过,经人提醒DataSet已自带读写XML的功能,于是便删了,不过在实践中感觉封装一层后,使用起来还是蛮方便的。故再次重发。using System;using System.Collections.
以前在博客上发过,经人提醒DataSet已自带读写XML的功能,于是便删了,
不过在实践中感觉封装一层后,使用起来还是蛮方便的。故再次重发。

img_a6339ee3e57d1d52bc7d02b338e15a60.gif using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Collections.Generic;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Text;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Data;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.IO;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Xml;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
namespace  XmlDesign
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif    
class XmlDatasetConvert
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        
//将xml对象内容字符串转换为DataSet
img_33d02437d135341f0800e3d415312ae8.gif
        public static DataSet ConvertXMLToDataSet(string xmlData)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            StringReader stream 
= null;
img_33d02437d135341f0800e3d415312ae8.gif            XmlTextReader reader 
= null;
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                DataSet xmlDS 
= new DataSet();
img_33d02437d135341f0800e3d415312ae8.gif                stream 
= new StringReader(xmlData);
img_33d02437d135341f0800e3d415312ae8.gif                
//从stream装载到XmlTextReader
img_33d02437d135341f0800e3d415312ae8.gif
                reader = new XmlTextReader(stream);
img_33d02437d135341f0800e3d415312ae8.gif                xmlDS.ReadXml(reader);
img_33d02437d135341f0800e3d415312ae8.gif                
return xmlDS;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch (System.Exception ex)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
throw ex;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
finally
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (reader != null) reader.Close();
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
//将xml文件转换为DataSet
img_33d02437d135341f0800e3d415312ae8.gif
        public static DataSet ConvertXMLFileToDataSet(string xmlFile)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            StringReader stream 
= null;
img_33d02437d135341f0800e3d415312ae8.gif            XmlTextReader reader 
= null;
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                XmlDocument xmld 
= new XmlDocument();
img_33d02437d135341f0800e3d415312ae8.gif                xmld.Load(xmlFile);
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif                DataSet xmlDS 
= new DataSet();
img_33d02437d135341f0800e3d415312ae8.gif                stream 
= new StringReader(xmld.InnerXml);
img_33d02437d135341f0800e3d415312ae8.gif                
//从stream装载到XmlTextReader
img_33d02437d135341f0800e3d415312ae8.gif
                reader = new XmlTextReader(stream);
img_33d02437d135341f0800e3d415312ae8.gif                xmlDS.ReadXml(reader);
img_33d02437d135341f0800e3d415312ae8.gif                
//xmlDS.ReadXml(xmlFile);
img_33d02437d135341f0800e3d415312ae8.gif
                return xmlDS;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch (System.Exception ex)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
throw ex;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
finally
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (reader != null) reader.Close();
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
//将DataSet转换为xml对象字符串
img_33d02437d135341f0800e3d415312ae8.gif
        public static string ConvertDataSetToXML(DataSet xmlDS)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            MemoryStream stream 
= null;
img_33d02437d135341f0800e3d415312ae8.gif            XmlTextWriter writer 
= null;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                stream 
= new MemoryStream();
img_33d02437d135341f0800e3d415312ae8.gif                
//从stream装载到XmlTextReader
img_33d02437d135341f0800e3d415312ae8.gif
                writer = new XmlTextWriter(stream, Encoding.Unicode);
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif                
//用WriteXml方法写入文件.
img_33d02437d135341f0800e3d415312ae8.gif
                xmlDS.WriteXml(writer);
img_33d02437d135341f0800e3d415312ae8.gif                
int count = (int)stream.Length;
img_33d02437d135341f0800e3d415312ae8.gif                
byte[] arr = new byte[count];
img_33d02437d135341f0800e3d415312ae8.gif                stream.Seek(
0, SeekOrigin.Begin);
img_33d02437d135341f0800e3d415312ae8.gif                stream.Read(arr, 
0, count);
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif                UnicodeEncoding utf 
= new UnicodeEncoding();
img_33d02437d135341f0800e3d415312ae8.gif                
return utf.GetString(arr).Trim();
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch (System.Exception ex)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
throw ex;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
finally
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (writer != null) writer.Close();
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
//将DataSet转换为xml文件
img_33d02437d135341f0800e3d415312ae8.gif
        public static void ConvertDataSetToXMLFile(DataSet xmlDS,string xmlFile)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif            MemoryStream stream 
= null;
img_33d02437d135341f0800e3d415312ae8.gif            XmlTextWriter writer 
= null;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                stream 
= new MemoryStream();
img_33d02437d135341f0800e3d415312ae8.gif                
//从stream装载到XmlTextReader
img_33d02437d135341f0800e3d415312ae8.gif
                writer = new XmlTextWriter(stream, Encoding.Unicode);
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif                
//用WriteXml方法写入文件.
img_33d02437d135341f0800e3d415312ae8.gif
                xmlDS.WriteXml(writer);
img_33d02437d135341f0800e3d415312ae8.gif                
int count = (int)stream.Length;
img_33d02437d135341f0800e3d415312ae8.gif                
byte[] arr = new byte[count];
img_33d02437d135341f0800e3d415312ae8.gif                stream.Seek(
0, SeekOrigin.Begin);
img_33d02437d135341f0800e3d415312ae8.gif                stream.Read(arr, 
0, count);
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif                
//返回Unicode编码的文本
img_33d02437d135341f0800e3d415312ae8.gif
                UnicodeEncoding utf = new UnicodeEncoding();
img_33d02437d135341f0800e3d415312ae8.gif                StreamWriter sw 
= new StreamWriter(xmlFile);
img_33d02437d135341f0800e3d415312ae8.gif                sw.WriteLine(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>");
img_33d02437d135341f0800e3d415312ae8.gif                sw.WriteLine(utf.GetString(arr).Trim());
img_33d02437d135341f0800e3d415312ae8.gif                sw.Close();
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
catch( System.Exception ex )
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
throw ex;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_33d02437d135341f0800e3d415312ae8.gif            
finally
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                
if (writer != null) writer.Close();
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_05dd8d549cff04457a6366b0a7c9352a.gif}

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
目录
相关文章
|
XML 数据格式
hutool将XML文档转换为String
hutool将XML文档转换为String
|
XML JavaScript API
Qt通过Doc模式读取XML并设计一个增删改查方便的操作类
Qt通过Doc模式读取XML并设计一个增删改查方便的操作类
174 0
|
XML Android开发 数据格式
Viewbinding自动生成XML的一个对应绑定类
设置完sync一下,然后会在项目中看到对应的XML文件的一个继承了ViewBinding的对应绑定类。 如果不想生成,则在XML文件中设置一个tools:viewBindingIgnore="true"属性,即可忽略不生成对应绑定类。
175 0
|
XML 数据格式
使用XStream进行对象和xml的转换,极度舒适
使用XStream进行对象和xml的转换,极度舒适
157 0
使用XStream进行对象和xml的转换,极度舒适
|
XML Java 数据安全/隐私保护
java采用注解方式将JavaBean转换为XML并采用AES全报文加密
java采用注解方式将JavaBean转换为XML并采用AES全报文加密
291 0
|
XML 数据格式
使用jaxb进行xml到bean的转换
使用jaxb进行xml到bean的转换(尝试解决空值不显示问题)
124 0
|
XML 数据格式
C#XmlHelper帮助类操作Xml文档的通用方法汇总(下)
C#XmlHelper帮助类操作Xml文档的通用方法汇总(下)
101 0
|
XML 数据格式
C#XmlHelper帮助类操作Xml文档的通用方法汇总(上)
C#XmlHelper帮助类操作Xml文档的通用方法汇总
136 0
|
XML JSON 数据格式
.NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)
.NET中XML序列化和反序列化常用类和用来控制XML序列化的属性总结(XmlSerializer,XmlTypeAttribute,XmlElementAttribute,XmlAttributeAttribute,XmlArrayAttribute...)
279 0
|
XML Java 数据格式
jav springboot XML转换工具类.
jav springboot XML转换工具类.