将数据导出到xml,并从xml导入数据

 

将数据导出到 xml ,并从 xml 导入数据
 
xml 本质上是数据,是另一种格式的数据。而 ADO.NET 就是用来处理数据的。因此通过 DataSet 可以非常方便的处理 xml 。下面这段程序对如下问题做出了解答:
 
怎样从DataSet得到xml?
生成的XML没有表之间的关系怎么办?
怎样把列数据作为子元素?(在利用数据集得到XML时)
怎样把列数据作为属性?(在利用数据集得到XML时)
怎样利用数据集形成XML文件?
怎样设置xml的namespace和prefix?
怎样控制控制文本的格式?
怎样把XML文件里的数据读入数据集?
怎样利用XML文件传递数据集架构?
怎样利用XML文件在不同数据集之间传递更改?
 
using System;
using System.Data;
using System.Data.OleDb;
using General.ADONET;
using General.ADONET.OLEDBClient;
using System.IO;
using System.Xml;
 
namespace PlayXML
{
     ///<summary>
     /// PlayXML 的摘要说明。
     ///</summary>
     public class XMLPlayer
     {
         public XMLPlayer()
         {
              //
              // TODO: 在此处添加构造函数逻辑
              //
         }
 
         private Adapter adptEmp;
         private Adapter adptDept;
         private OleDbConnection conn;
         private DataSet dsHR;
 
          //初始化数据集
         public void InitDataSet()
         {
            string strConn = "Provider = Microsoft.Jet.OLEDB.4.0;";
              strConn += "Data Source = E://everyday//VS//HELPREN//hr.mdb;";
              this.conn = new OleDbConnection(strConn);
 
              dsHR = new DataSet();
 
              this.adptEmp =
                   new Adapter(this.conn, dsHR, "employees");
              this.adptDept =
                   new Adapter(this.conn, dsHR, "departments");
 
              this.adptDept.Fill();
              this.adptEmp.Fill();
 
              DataTable dtDept = this.dsHR.Tables["departments"];
              DataTable dtEmp = this.dsHR.Tables["employees"];
 
              DataColumn colParent = dtDept.Columns["dept_id"];
              DataColumn colChild = dtEmp.Columns["emp_department"];
 
              dsHR.Relations.Add(
                   "departments_employees",
                   colParent, colChild);//关系
         }
 
         //怎样从DataSet得到xml?
         public void GetXml()
         {
              Console.WriteLine(
                   this.dsHR.GetXml()//这个方法就是从DataSet得到xml
                   );
         }
 
         //生成的XML没有表之间的关系怎么办?
         public void GetXmlWithRalation()
         {
              //指定DataRelation在DataSet中进行嵌套
              //使xml序列化生成关系
              this.dsHR.Relations["departments_employees"].Nested = true;
 
              Console.WriteLine(
                   this.dsHR.GetXml()//这个方法就是从DataSet得到xml
                   );
         }
 
         //怎样把列数据作为子元素?(在利用数据集得到XML时)
         public void GetSubElements()
         {
              DataTable dtEmp = this.dsHR.Tables["employees"];
              foreach(DataColumn col in dtEmp.Columns)
              {
                   //列的ColumnMapping属性设置为MappingType.Element就可以了
                   col.ColumnMapping = MappingType.Element;
              }
                  
              DataTable dtDept = this.dsHR.Tables["departments"];
              foreach(DataColumn col in dtDept.Columns)
              {
                   //列的ColumnMapping属性设置为MappingType.Element就可以了
                   col.ColumnMapping = MappingType.Element;
              }
 
              this.dsHR.GetXml();
         }
 
         //怎样把列数据作为属性?(在利用数据集得到XML时)
         public void GetAttributes()
         {
              DataTable dtEmp = this.dsHR.Tables["employees"];
              foreach(DataColumn col in dtEmp.Columns)
              {
                   //列的ColumnMapping属性设置为MappingType.Attribute就可以了
                   col.ColumnMapping = MappingType.Attribute;
              }
                  
              DataTable dtDept = this.dsHR.Tables["departments"];
              foreach(DataColumn col in dtDept.Columns)
              {
                   //列的ColumnMapping属性设置为MappingType.Attribute就可以了
                   col.ColumnMapping = MappingType.Attribute;
              }
 
              this.dsHR.GetXml();
         }
 
         //怎样利用数据集形成XML文件?
         public void WriteXml()
         {
              //怎样设置xml的namespace和prefix?
              this.dsHR.Namespace = "ADONET";
              this.dsHR.Prefix = "an";
 
              this.dsHR.WriteXml("test.xml", XmlWriteMode.WriteSchema);
 
              //怎样控制控制文本的格式?
              StreamWriter sw = new StreamWriter("test2.xml");
              XmlTextWriter xmlWriter = new XmlTextWriter(sw);
              xmlWriter.Formatting = Formatting.Indented;
              xmlWriter.Indentation = 10;
              this.dsHR.WriteXml(xmlWriter);
              xmlWriter.Close();         
         }
 
         //怎样把XML文件里的数据读入数据集?
         public void ReadXml()
         {
              DataSet dsNew = new DataSet();
              dsNew.ReadXml("test.xml");
              Console.WriteLine(dsNew.GetXml());
         }
 
         //怎样利用XML文件传递数据集架构?
         public void TransmitSchema()
         {
              StreamWriter sw = new StreamWriter("test.xsd");
              XmlTextWriter xmlWriter = new XmlTextWriter(sw);
              this.dsHR.WriteXmlSchema(xmlWriter);
              xmlWriter.Close();
 
              DataSet dsNew = new DataSet();
              dsNew.ReadXmlSchema("test.xsd");
 
              foreach(DataTable dt in dsNew.Tables)
              {
                   Console.WriteLine(dt.TableName);
                   foreach(DataColumn dc in dt.Columns)
                   {
                       Console.WriteLine("         " + dc.ColumnName);
                   }
              }
         }
 
         //怎样利用XML文件在不同数据集之间传递更改?
         public void PlayDiffGram()
         {
              //为两个datatable设置主键
              DataTable dtEmp = this.dsHR.Tables["employees"];
              DataTable dtDept = this.dsHR.Tables["departments"];
              dtEmp.PrimaryKey = new DataColumn[]
                   {dtEmp.Columns["emp_id"]};
              dtDept.PrimaryKey = new DataColumn[]
                   {dtDept.Columns["dept_id"]};
 
              //把数据集写到xml文件
              StreamWriter sw = new StreamWriter("test.xml");
              XmlTextWriter xmlWriter = new XmlTextWriter(sw);
              this.dsHR.WriteXml(xmlWriter, XmlWriteMode.WriteSchema);
              sw.Close();
 
              //从xml文件读入数据到一个新的数据集里
              DataSet dsNew = new DataSet();
              dsNew.ReadXml("test.xml", XmlReadMode.ReadSchema);
              dsNew.AcceptChanges();
 
              //修改值
              this.dsHR.Tables["employees"].Rows[0]["emp_name"] = "aaaaa";
              dsNew.Tables["employees"].Rows[0]["emp_name"] = "ccccc";
 
              //把新数据集里的数据输出到xml文件
              Stream newStream = new MemoryStream() as Stream;
              XmlTextWriter xmlNewWriter =
                   new XmlTextWriter(newStream, null);
              xmlNewWriter.Formatting = Formatting.Indented;
              dsNew.GetChanges().WriteXml(xmlNewWriter, XmlWriteMode.DiffGram);
 
              //显示现在xml文件的内容
              newStream.Position = 0;
              StreamReader rdr = new StreamReader(newStream);
              Console.WriteLine(rdr.ReadToEnd());
 
              newStream.Position = 0;
 
              //把现在的xml文件的内容读到原先的那个数据集
              this.dsHR.ReadXml(newStream, XmlReadMode.DiffGram);
         }
     }
}
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QML中可以通过Qt Quick XML模块来完成XML文件的导入导出和修改。下面是一个简单的示例代码,演示了如何在QML中使用Qt Quick XML模块操作XML文件。 首先,在QML文件的头部添加以下代码,导入Qt Quick XML模块: ``` import QtQuick.XmlListModel 2.0 ``` 然后,使用XmlListModel组件来加载XML文件,并将其显示在ListView中: ``` XmlListModel { id: xmlModel source: "file.xml" query: "/root/item" XmlRole { name: "title"; query: "title/string()" } XmlRole { name: "description"; query: "description/string()" } } ListView { model: xmlModel delegate: Text { text: title } } ``` 在以上代码中,XmlListModel组件通过source属性指定要加载的XML文件,query属性指定要查询的节点路径,XmlRole定义了节点中要显示的数据项。 接下来,可以在QML中使用JavaScript来修改XML文件。以下是一个示例代码,演示了如何添加一个新的节点: ``` XmlListModel { id: xmlModel source: "file.xml" query: "/root/item" XmlRole { name: "title"; query: "title/string()" } XmlRole { name: "description"; query: "description/string()" } } function addItem(title, description) { var newElement = xmlModel.createObject({"title": title, "description": description}) xmlModel.append(newElement) xmlModel.save() } Button { text: "Add Item" onClicked: addItem("New Item", "New Description") } ``` 在以上代码中,addItem函数使用createObject方法创建一个新的XML节点,并将其添加到XmlListModel中。最后,调用save方法将修改保存到XML文件中。 以上就是在QML中导入导出和修改XML文件的基本方法。需要注意的是,Qt Quick XML模块的使用需要在Qt项目中添加xml模块的依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值