using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Data;
using System.IO;
using System.Windows.Forms;
//modify 2013-08-21 weiyj 修改XML文件格式
namespace yjs_cl_seq
{
class XMLHelper
{
public void createXml(string DocTitle, string path, DataTable sampTable, DataTable zfTable, string CurrentUserName, string netUser, string netPwd, string netIP)
{
try
{
path = path + @"" + CurrentUserName;
try
{
//访问的是网络路径
if (!string.IsNullOrEmpty(netUser) && !string.IsNullOrEmpty(netPwd) && !string.IsNullOrEmpty(netIP))
{
if (ConnectHelper.impersonateValidUser(netUser, netIP, netPwd) == false)
{
MessageBox.Show("网络路径不存在");
return;
}
}
//访问的是本地路径
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch (Exception ex)
{
MessageBox.Show("访问路径错误" + ex.ToString());
return;
}
int indexID = 0;
XmlTextWriter writer = new XmlTextWriter(path + @"" + DocTitle + "_" + DateTime.Now.ToString("yyyy-MM-dd hhmmssfff") + ".xml", null);
//使用自动缩进便于阅读
writer.Formatting = Formatting.Indented;
//写入根元素
//modify by weiyj 2013-08-22
writer.WriteStartElement("Samples");
if (sampTable != null && sampTable.Rows.Count > 0)
{
for (int i = 0; i < sampTable.Rows.Count; i++)
{
int j = 1;
int.TryParse(sampTable.Rows[i]["进样次数"].ToString(), out j);
int k = 0;
while (k < j)
{
writer.WriteStartElement("Sample");
//写入属性及属性的名字
//加入子元素
//modify by weiyj 2013-08-21 修改XML文件格式
writer.WriteElementString("Number", (++indexID).ToString());
writer.WriteElementString("Location", "1");
writer.WriteElementString("Name", sampTable.Rows[i]["名称"].ToString());
writer.WriteElementString("CDSMethod", string.Empty);
writer.WriteElementString("numberOfInj", "1");
writer.WriteElementString("sampleType", "Standard");
writer.WriteElementString("CalLevel", string.Empty);
writer.WriteElementString("calibration", string.Empty);
writer.WriteElementString("UpdateRT", string.Empty);
writer.WriteElementString("Interval", string.Empty);
writer.WriteElementString("sampleAmount", string.Empty);
writer.WriteElementString("Multipliers", string.Empty);
writer.WriteElementString("Dilution", string.Empty);
writer.WriteElementString("DataFilename", string.Empty);
writer.WriteElementString("InjectionVolume", string.Empty);
writer.WriteElementString("description", string.Empty);
writer.WriteElementString("StudyName", string.Empty);
writer.WriteElementString("LimsID", sampTable.Rows[i]["别名"].ToString() + "/" + sampTable.Rows[i]["项目"].ToString() + "/" + sampTable.Rows[i]["称量次数"].ToString());
writer.WriteElementString("LimsKField2", sampTable.Rows[i]["项目编号"].ToString());
writer.WriteElementString("LimsKField3", "Standard");
//end by weiyj 2013-08-21
//关闭根元素,并书写结束标签
writer.WriteEndElement();
k++;
}
}
}
if (zfTable != null && zfTable.Rows.Count > 0)
{
for (int i = 0; i < zfTable.Rows.Count; i++)
{
int j = 1;
int.TryParse(zfTable.Rows[i]["进样次数"].ToString(), out j);
int k = 0;
while (k < j)
{
writer.WriteStartElement("Sample");
//写入属性及属性的名字
//加入子元素
//modify by weiyj 2013-08-21 修改XML文件格式
writer.WriteElementString("Number", (++indexID).ToString().ToString());
writer.WriteElementString("Location", "1");
writer.WriteElementString("Name", zfTable.Rows[i]["检品编号"].ToString());
writer.WriteElementString("CDSMethod", string.Empty);
writer.WriteElementString("numberOfInj", "1");
writer.WriteElementString("sampleType", "SAMPLE");
writer.WriteElementString("CalLevel", string.Empty);
writer.WriteElementString("calibration", string.Empty);
writer.WriteElementString("UpdateRT", string.Empty);
writer.WriteElementString("Interval", string.Empty);
writer.WriteElementString("sampleAmount", string.Empty);
writer.WriteElementString("Multipliers", string.Empty);
writer.WriteElementString("Dilution", string.Empty);
writer.WriteElementString("DataFilename", string.Empty);
writer.WriteElementString("InjectionVolume", string.Empty);
writer.WriteElementString("description", string.Empty);
writer.WriteElementString("StudyName", string.Empty);
writer.WriteElementString("LimsID", zfTable.Rows[i]["检品名称"].ToString() + "/" + zfTable.Rows[i]["项目"].ToString() + "/" + zfTable.Rows[i]["称量次数"].ToString());
writer.WriteElementString("LimsKField2", zfTable.Rows[i]["项目编号"].ToString());
writer.WriteElementString("LimsKField3", "Sample");
//end by weiyj 2013-08-21
//关闭根元素,并书写结束标签
writer.WriteEndElement();
k++;
}
}
}
//end by weiyj 2013-08-22
writer.WriteEndElement();
//将XML写入文件并且关闭XmlTextWriter
writer.Close();
}
catch (Exception)
{
throw;
}
}
public void addItem()
{
// XmlDocument xmlDoc = new XmlDocument();
// xmlDoc.Load("titles.xml");
// XmlNode root = xmlDoc.SelectSingleNode("items");
// XmlElement xe1 = xmlDoc.createElement_x("item");
// xe1.SetAttribute("类别", "张三");
// xe1.SetAttribute("品质", "老师");
// //XmlElement xmlsub1 = xmlDoc.createElement_x("title");
// xmlsub1.InnerText = "wahahha"; xe1.AppendChild(xmlsub1);
//// XmlElement xmlsub2 = xmlDoc.createElement_x("author");
// xmlsub2.InnerText = "三毛";
// xe1.AppendChild(xmlsub2);
// //XmlElement xmlsub3 = xmlDoc.createElement_x("Price");
// xmlsub3.InnerText = "15.00";
// xe1.AppendChild(xmlsub3);
// root.AppendChild(xe1);
// xmlDoc.Save("titles.xml");
}
public void readtext()
{
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("titles.xml");
XmlNode xn = xmlDoc.SelectSingleNode("items");
XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
Console.Write("类别 " + xe.GetAttribute("类别") + " 品质 ");//显示属性值 Console.WriteLine(xe.GetAttribute("品质")); XmlNodeList xnf1 = xe.ChildNodes; foreach (XmlNode xn2 in xnf1) {
//Console.WriteLine(xn2.InnerText);//显示子节点点文本 }
Console.WriteLine();
}
}
public void updatexml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("titles.xml");
XmlNode xns = xmlDoc.SelectSingleNode("items");
XmlNodeList xnl = xns.ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("类别") == "文学")
{
xe.SetAttribute("类别", "娱乐");
}
else
{
xe.SetAttribute("类别", "文学");
}
XmlNodeList xnl2 = xe.ChildNodes; foreach (XmlNode xn2 in xnl2)
{
XmlElement xe2 = (XmlElement)xn2; if (xe2.Name == "price")
{
if (xe2.InnerText == "10.00") xe2.InnerText = "15.00";
else
xe2.InnerText = "10.00";
}
xmlDoc.Save("titles.xml");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
readtext();
}
}
}
public void delnode()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("titles.xml");
XmlNodeList xnl = xmlDoc.SelectSingleNode("items").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("类别") == "文学")
{
xe.RemoveAttribute("类别");//删除genre属性
}
else if (xe.GetAttribute("类别") == "娱乐")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("titles.xml");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
readtext();
}
}
}