DataTable 与 XML ---DataTable 读写XML

前端之家收集整理的这篇文章主要介绍了DataTable 与 XML ---DataTable 读写XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



用法 :
创建结构
        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>


原文链接:https://www.f2er.com/xml/296919.html

猜你在找的XML相关文章