加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

DataTable 与 XML ---DataTable 读写XML

发布时间:2020-12-16 06:22:51 所属栏目:百科 来源:网络整理
导读:用法 :创建结构 DataClass.dtXml xml = new DataClass.dtXml(DataClass.sc.strXmlFile);xml.createSchema(); //只创建表的结构写入XML currentDt.AcceptChanges(); currentDt.WriteXml(DataClass.sc.strXmlFile,XmlWriteMode.WriteSchema);读XML文件 current



用法 :
创建结构
        DataClass.dtXml xml = new DataClass.dtXml(DataClass.sc.strXmlFile);	
	xml.createSchema(); //只创建表的结构

写入XML
            currentDt.AcceptChanges();
            currentDt.WriteXml(DataClass.sc.strXmlFile,XmlWriteMode.WriteSchema);

读XML文件
               currentDt.ReadXml(DataClass.sc.strXmlFile);
               currentDv = currentDt.DefaultView ;
               mdl.viewDataInGridView(dataGridView1,currentDv,bindingNavigator1,bindingSource1);

//-----------------------------------------------------------------------------------------------------------------


  public class dtXml
   {

     private static string fName;

     public dtXml( string _fName)
     {
        fName = _fName;
     }

     //public  
     public void createSchema()
     {
        DataTable table = new DataTable("reason" );
        DataColumn column = new DataColumn("id",typeof(System.Int32));
        column.AutoIncrement = true;
        table.Columns.Add(column);

        column = new DataColumn("item",typeof(System.String));
        table.Columns.Add(column);

        table.WriteXmlSchema(fName);

     }



      public   void DemonstrateReadWriteXMLDocumentWithString()
      {
         DataTable table = CreateTestTable("Reason");
         PrintValues(table,"Original table");

         //string fileName = "d:TestData.xml";
         table.WriteXml( fName,XmlWriteMode.WriteSchema);

         DataTable newTable = new DataTable();
         newTable.ReadXml(fName);

         // Print out values in the table.
         PrintValues(newTable,"New table");
      }

      private  DataTable CreateTestTable(string tableName)
      {
         // Create a test DataTable with two columns and a few rows.
         DataTable table = new DataTable(tableName);
         DataColumn column = new DataColumn("id",typeof(System.Int32));
         column.AutoIncrement = true;
         table.Columns.Add(column);

         column = new DataColumn("item",typeof(System.String));
         table.Columns.Add(column);

         // Add ten rows.
         DataRow row;
         for (int i = 0; i <= 9; i++)
         {
            row = table.NewRow();
            row["item"] = "item " + i;
            table.Rows.Add(row);
         }

         table.AcceptChanges();
         return table;
      }

      private  void PrintValues(DataTable table,string label)
      {
         Console.WriteLine(label);
         foreach (DataRow row in table.Rows)
         {
            foreach (DataColumn column in table.Columns)
            {
               Console.Write("t{0}",row[column]);
            }
            Console.WriteLine();
         }
      }

   }

生成的 XML 文件格式如下:


<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="reason" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="reason">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="id" msdata:AutoIncrement="true" type="xs:int" minOccurs="0" />
                <xs:element name="item" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <reason>
    <id>0</id>
    <item>错送</item>
  </reason>
  <reason>
    <id>1</id>
    <item>有问题</item>
  </reason>
  <reason>
    <id>2</id>
    <item>短重</item>
  </reason>
  <reason>
    <id>3</id>
    <item>破损</item>
  </reason>
  <reason>
    <id>4</id>
    <item>送少</item>
  </reason>
  <reason>
    <id>5</id>
    <item>原因不明</item>
  </reason>
  <reason>
    <id>6</id>
    <item>原因很多</item>
  </reason>
</NewDataSet>


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读