构建XML文件,压缩并保存二进制文件至数据库(使用ODBC方式)

编程

using Ionic.Zip;

using System;

using System.Collections;

using System.Collections.Generic;

using System.Data;

using System.IO;

using System.IO.Compression;

using System.Text;

using System.Xml;

namespace ConTestODBC

{

public class test1

{

OdbcHelper odbcHelper = new OdbcHelper();

public static void test2()

{

DataTable dtTag = new DataTable();

dtTag.Columns.Add("time");

dtTag.Columns.Add("amp");

dtTag.Rows.Add("2019/11/12 18:03:09", "TI-0910104/AI1/PV.CV");

dtTag.Rows.Add("2019/11/12 18:03:09", "TI-0910704/AI1/PV.CV");

dtTag.Rows.Add("2019/11/12 18:03:09", "TI-1200102/AI1/PV.CV");

dtTag.Rows.Add("2019/11/12 18:03:09", "TI-1224005/AI1/PV.CV");

dtTag.Rows.Add("2019/11/12 18:03:09", "TI-1820101/AI1/PV.CV");

//创建XmlDocument对象

XmlDocument xmlDoc = new XmlDocument();

//XML的声明<?xml version="1.0" encoding="gb2312"?>

XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);

//追加xmldecl位置

xmlDoc.AppendChild(xmlSM);

//添加一个名为TraceData的根节点

XmlElement xml = xmlDoc.CreateElement("", "TraceData", "");

//追加TraceData的根节点位置

xmlDoc.AppendChild(xml);

//添加另一个节点,与TraceData所匹配,查找<TraceData>

XmlNode traceData = xmlDoc.SelectSingleNode("TraceData");

//添加一个名为<Header>的节点

XmlElement Header = xmlDoc.CreateElement("Header");

//为<Header>节点的属性

//Header.SetAttribute("name", "博客园");

//Header.SetAttribute("age", "26");

XmlElement x1 = xmlDoc.CreateElement("ColumnHeader");

//InnerText:获取或设置节点及其所有子节点的串连值

x1.InnerText =dtTag.Columns[0].ToString()+","+ dtTag.Columns[1].ToString();//"time,amp"

Header.AppendChild(x1);//添加到<Header>节点中

traceData.AppendChild(Header);//添加到<TraceData>节点中

XmlElement Data = xmlDoc.CreateElement("Data");

for (int i = 0; i < dtTag.Rows.Count; i++)

{

Data.InnerText += Environment.NewLine+" ";

for (int j = 0; j < dtTag.Columns.Count; j++)

{

Data.InnerText += dtTag.Rows[i][j].ToString();//Environment.NewLine

}

}

Data.InnerText += Environment.NewLine;

//为<Data>节点的属性

//Data.SetAttribute("name", "博客园");

//Data.SetAttribute("age", "26");

//XmlElement x2 = xmlDoc.CreateElement("ColumnHeader");

////InnerText:获取或设置节点及其所有子节点的串连值

//x1.InnerText = "time,amp";

//Header.AppendChild(x1);//添加到<Header>节点中

traceData.AppendChild(Data);//添加到<TraceData>节点中

//保存好创建的XML文档

string sFilePath = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");// Application.StartupPath + "\UserFile\Label\" + DateTime.Now.ToString();

xmlDoc.Save("D:/OpcXML/data.xml");

Console.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));

}

//

public static void SaveopcXML()

{

//DirectoryInfo dir = new DirectoryInfo("D:/OpcXML");//GlobalVariable.g_LogInfo.sLogDir

//FileInfo[] files = dir.GetFiles();

//List<FileInfo> files1 = new List<FileInfo>();

//for (int i = 0; i < files.Length; i++)

//{

// files1.Add(files[i]);

//}

FileStream fs = new FileStream("D:/OpcXML/Data.xml", FileMode.Open);

BinaryReader br = new BinaryReader(fs);

Byte[] byData = br.ReadBytes((int)fs.Length);

int fLength = (int)fs.Length;

Byte[] zipData = Compress(byData).ToArray();

OdbcHelper.OracleExeQuery("update U_WIPPROCDATA set FILE_DATA=RAWTOHEX("" + zipData + ""), CREATE_TIME=sysdate,CREATE_USER_ID="11" where FACTORY ="15车间" AND LOT_ID ="1"");

//RAWTOHEX()将byte数组转换为16进制,避免报错无效的16进制数字

fs.Close();

Console.WriteLine(fLength);

}

public static MemoryStream Compress(byte[] inBytes)

{

MemoryStream outStream = new MemoryStream();

using (MemoryStream intStream = new MemoryStream(inBytes))

{

using (GZipStream Compress = new GZipStream(outStream, CompressionMode.Compress))

{

intStream.CopyTo(Compress);

}

}

return outStream;

}

}

}

 

以上是 构建XML文件,压缩并保存二进制文件至数据库(使用ODBC方式) 的全部内容, 来源链接: utcz.com/z/510683.html

回到顶部