/// <summary>
/// 读取文件填充信息到对应的字段
/// </summary>
/// <param name="filePath"></param>
/// <param name="dict"></param>
/// <param name="hasDZP"></param>
/// <param name="isJPBHINDZP">对照品选取条件</param>
public bool ReadXmlFile(string filePath, Dictionary<string, string> dict, bool hasDZP, bool isJPBHINDZP)
{
bool flag = false;
//要读取的是中文
StreamReader reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath), Encoding.Default);
XDocument xDoc = XDocument.Load(reader);
//判断是否是支持Sequence的文件格式
bool isSequence = IsSequence(xDoc);
//将值填充到datagridview的列中
if (isSequence)
{
#region 支持Sequence的文件格式
flag = ReadXMLFileOfSequence(xDoc);
#endregion
}
else
{
#region 不支持Sequence的文件格式
flag = ReadXMLFileNoSequence(dict, xDoc, hasDZP, isJPBHINDZP);
#endregion
}
if (reader != null) {
reader.Close();
reader.Dispose();
}
return flag;
}
/// <summary>
/// 判断是否是支持Sequence的文件
/// </summary>
/// <param name="xDoc"></param>
/// <param name="isSequence"></param>
/// <returns></returns>
public bool IsSequence(XDocument xDoc)
{
//1 要读取的文件有两种格式,根据是否有<xml>节点来判断
bool isSequence = true;
var queryNodes = (from nodes in xDoc.Document.Nodes() select nodes).Distinct();
foreach (XNode node in queryNodes)
{
XElement xe = (XElement)node;
if (xe.Name == "xml")
{
isSequence = true;
}
if (xe.Name == "lims_results")
{
isSequence = false;
}
}
return isSequence;
}
/// <summary>
/// 读取支持Sequence的文件格式
/// </summary>
/// <param name="xDoc"></param>
public bool ReadXMLFileOfSequence(XDocument xDoc)
{
bool flag = false;
try
{
//对照品中在同一个test节点中testid是否存在的标记
List<string> testList = new List<string>();
#region 对照品
for (int i = 0; i < ControlArticle_TestID_List.Count; i++)
{
int indexIDControl = dataGridViewControlAtricle.Rows.Count;
int rowCountControl = indexIDControl;
var testArticlequery = (from element in xDoc.Document.Element("xml").Element("samples").Elements("sample").Elements("test").Elements()
where element.Parent.Parent.Element("type").Attribute("id").Value != null
&& element.Parent.Parent.Element("type").Attribute("id").Value.ToString().ToUpper() == "STANDARD"
&& element.Parent.Parent.Element("test").Attribute("name").Value != null
&& element.Parent.Parent.Element("test").Attribute("name").Value.ToString().Contains(ControlArticle_TestID_List[i])
&& ControlArticle_TestID_List[i] != ""
select element).Distinct();
string test = "";
foreach (XElement el in testArticlequery)
{
test = el.Parent.Parent.Element("test").Attribute("name").Value;
}
if (!testList.Contains(test))
{
testList.Add(test);
}
else
{
continue;
}
foreach (XElement el in testArticlequery)
{
test = el.Parent.Parent.Element("test").Attribute("name").Value;
//检品编号
string sample = el.Parent.Parent.Attribute("name").Value;
//<description info=”STD1/有关物质/01”></description> 代表样品名(含有份数),解析使用左斜杠“/”分割的数据
string description = el.Parent.Parent.Element("description").Attribute("info").Value;
//代表检项编号,解析使用分号“;”来分割的数据
string[] sDescription = description.Split(new char[] { '/' });
string anotherName = string.Empty;
string project = string.Empty;
string weighTimes = string.Empty;
if (sDescription.Length > 0)
{
anotherName = sDescription[0].ToString();//第一段即为别名
}
if (sDescription.Length > 1)
{
project = sDescription[1].ToString();//第二段为项目
}
if (sDescription.Length > 2)
{
weighTimes = sDescription[2].ToString();//第三段为称量次数
}
if (el.Attribute("name").Value != string.Empty)
{
flag = true;
dataGridViewControlAtricle.Rows.Add();
rowCountControl = rowCountControl + 1;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["XuHao"].Value = rowCountControl;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["AnotherName"].Value = anotherName;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["Name"].Value = sample;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["ChengliangCS"].Value = weighTimes;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["component_name"].Value = el.Attribute("name").Value;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["PeakValue"].Value = el.Attribute("value").Value;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["Project"].Value = project;
dataGridViewControlAtricle.Rows[indexIDControl].Cells["Check"].Value = true;
indexIDControl = indexIDControl + 1;
}
}
}
#endregion
#region 供试品
for (int i = 0; i < TheSample_TestArticleNo_List.Count; i++)
{
int indexIDSample = dataGridViewTheSample.Rows.Count;
int rowCountSample = indexIDSample;
//<type id="Sample"> 并且检品编号存在
var testArticlequery = (from element in xDoc.Document.Element("xml").Element("samples").Elements("sample").Elements("test").Elements()
where element.Parent.Parent.Element("type").Attribute("id").Value != null
&& element.Parent.Parent.Element("type").Attribute("id").Value.ToString().ToUpper() == "SAMPLE"
&& element.Parent.Parent.Attribute("name").Value != null
&& element.Parent.Parent.Attribute("name").Value.ToString().Contains(TheSample_TestArticleNo_List[i])
&& TheSample_TestArticleNo_List[i] != ""
select element).Distinct();
foreach (XElement el in testArticlequery)
{
//检品编号
string sample = el.Parent.Parent.Attribute("name").Value;
//<description info=”STD1/有关物质/01”></description> 代表样品名(含有份数),解析使用左斜杠“/”分割的数据
string description = el.Parent.Parent.Element("description").Attribute("info").Value;
//代表检项编号,解析使用分号“;”来分割的数据
string test = el.Parent.Parent.Element("test").Attribute("name").Value;
string[] sDescription = description.Split(new char[] { '/' });
string anotherName = string.Empty;
string project = string.Empty;
string weighTimes = string.Empty;
if (sDescription.Length > 0)
{
anotherName = sDescription[0].ToString();//第一段即为别名
}
if (sDescription.Length > 1)
{
project = sDescription[1].ToString();//第二段为项目
}
if (sDescription.Length > 2)
{
weighTimes = sDescription[2].ToString();//第三段为称量次数
}
if (el.Attribute("name").Value != string.Empty)
{
flag = true;
//dataGridViewTheSample.Rows.Add(++indexIDSample, sample, anotherName, weighTimes, el.Attribute("name").Value, el.Attribute("value").Value, project);
dataGridViewTheSample.Rows.Add();
rowCountSample = rowCountSample + 1;
dataGridViewTheSample.Rows[indexIDSample].Cells["XuHao"].Value = rowCountSample;
dataGridViewTheSample.Rows[indexIDSample].Cells["sample_name"].Value = sample;
dataGridViewTheSample.Rows[indexIDSample].Cells["TheSampleNo"].Value = anotherName;
dataGridViewTheSample.Rows[indexIDSample].Cells["ChengliangCS"].Value = weighTimes;
dataGridViewTheSample.Rows[indexIDSample].Cells["component_name"].Value = el.Attribute("name").Value;
dataGridViewTheSample.Rows[indexIDSample].Cells["PeakValue"].Value = el.Attribute("value").Value;
dataGridViewTheSample.Rows[indexIDSample].Cells["Project"].Value = project;
dataGridViewTheSample.Rows[indexIDSample].Cells["Check"].Value = true;
indexIDSample = indexIDSample + 1;
}
}
}
#endregion
}
catch (Exception ex)
{
MessageBox.Show("读取支持Sequence的文件格式错误" + ex.ToString());
}
return flag;
}