从DataTable写入Xml和从Xml返回到DataTable

一.从DataTable写入Xml

private void button1_Click(object sender, System.EventArgs e)
   {
    DataTable dtTCCL0001 = QueryTCCL0001();   
    string strFile = "D:\\TCCL0001.xml";
    XmlTextWriter objXml = new XmlTextWriter(strFile,Encoding.UTF8);
    objXml.WriteStartDocument();
    objXml.WriteStartElement("InventoryXML");
    objXml.WriteElementString("TransferID","123");
    for(int i=0;i<dtTCCL0001.Rows.Count;i++)
    {   
     objXml.WriteStartElement("Inventory");
     objXml.WriteElementString("WareHouseNo",dtTCCL0001.Rows[i]["WareHouseNo"].ToString());
     objXml.WriteElementString("CustomerCode",dtTCCL0001.Rows[i]["CustomerCode"].ToString());
     objXml.WriteElementString("CustomerPartNo",dtTCCL0001.Rows[i]["CustomerPartNo"].ToString());
     objXml.WriteElementString("CustomerPartNoVersion",dtTCCL0001.Rows[i]["CustomerPartNoVersion"].ToString());
     objXml.WriteElementString("VendorCode",dtTCCL0001.Rows[i]["VendorCode"].ToString());
     objXml.WriteElementString("VendorName",dtTCCL0001.Rows[i]["VendorName"].ToString());
     objXml.WriteElementString("VendorPartNo",dtTCCL0001.Rows[i]["VendorPartNo"].ToString());
     objXml.WriteElementString("VendorPartNoVersion",dtTCCL0001.Rows[i]["VendorPartNoVersion"].ToString());
     objXml.WriteElementString("MFGCode",dtTCCL0001.Rows[i]["MFGCode"].ToString());
     objXml.WriteElementString("InventoryQty",dtTCCL0001.Rows[i]["InventoryQty"].ToString());
     objXml.WriteElementString("PartStatus",dtTCCL0001.Rows[i]["PartStatus"].ToString());
     objXml.WriteElementString("OnWayQty",dtTCCL0001.Rows[i]["OnWayQty"].ToString());
     objXml.WriteElementString("UOM",dtTCCL0001.Rows[i]["UOM"].ToString());
     objXml.WriteElementString("Description",dtTCCL0001.Rows[i]["Description"].ToString());
     objXml.WriteElementString("Ref1",dtTCCL0001.Rows[i]["Ref1"].ToString());
     objXml.WriteElementString("Ref2",dtTCCL0001.Rows[i]["Ref2"].ToString());
     objXml.WriteElementString("Ref3",dtTCCL0001.Rows[i]["Ref3"].ToString());
     objXml.WriteElementString("Ref4",dtTCCL0001.Rows[i]["Ref4"].ToString());
     objXml.WriteEndElement();  
    }
    objXml.WriteEndElement();  
    objXml.WriteEndDocument();  
    objXml.Flush();  
    objXml.Close();
    txtMessage2.Text = "转换成功";
   }

 

二.从Xml返回到DataTable

private void Button1_Click(object sender, System.EventArgs e)
   {
    XmlTextReader xtr = new XmlTextReader(@"D:\\TCCL0001.xml");
    while(xtr.Read())
    {
     if(xtr.NodeType == XmlNodeType.Element)
     {
      switch(xtr.LocalName)      
      {
       case "TransferID":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "WareHouseNo":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "CustomerCode":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "CustomerPartNo":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "CustomerPartNoVersion":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "VendorCode":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "VendorName":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "VendorPartNo":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "VendorPartNoVersion":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "MFGCode":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "InventoryQty":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "PartStatus":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "OnWayQty":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "UOM":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "Description":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "Ref1":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "Ref2":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "Ref3":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
       case "Ref4":
        HttpContext.Current.Response.Write(xtr.ReadString()+ "<br>");
        break;
      }
     }
    
    }
   }

你可能感兴趣的:(Datatable)