如何在 datagridview c# 中显示完整的 xml 文件
How to show full xml file in datagirdview c#
所以我有一个包含许多 XML 文件的目录。我试图在 datagirdview 中显示数据集,但内容不足,请有人帮我显示文件中的所有内容
下面是我使用的代码和XML。
<?xml version="1.0" encoding="UTF-8"?>
<ns1:BoardTestXMLExport numberOfIndictedComponents="4" testerTestStartTime="2021-01-18T07:46:32.000+07:00" testTime="2021-01-18T07:46:24.000+07:00" repairStationId="VNHCMING100" testStatus="Repaired" testerTestEndTime="2021-01-18T07:46:37.000+07:00" xmlns:ns1="http://tempuri.org/BoardTestXMLExport.xsd" numberOfIndictedPins="0" numberOfComponentsTested="320" numberOfJointsTested="0" numberOfDefects="4" repairStatus="Repaired">
<ns1:BoardXML imageId="3" serialNumber="21017227600" assemblyRevision="ING-296269012AC-A-B" boardType="ING-296269012AC-A-B" boardRevision="1610927415000"/>
<ns1:StationXML testerName="HCMINGAOI02" stage="V510"/>
<ns1:RepairEventXML numberOfVariationOkDefects="0" numberOfFalseCalledPins="0" numberOfRepairedComponents="2" numberOfVariationOkPins="0" numberOfRepairedPins="0" numberOfRepairLaterPins="0" numberOfFalseCalledDefects="2" numberOfActiveDefects="0" numberOfVariationOkComponents="0" repairEndTime="2021-01-18T07:49:24.000+07:00" repairStartTime="2021-01-18T07:49:10.000+07:00" numberOfRepairLaterDefects="0" repairOperator="c_admin" numberOfRepairLaterComponents="0" numberOfActiveComponents="0" numberOfActivePins="0" numberOfRepairedDefects="2" numberOfFalseCalledComponents="2"/>
<ns1:TestXML name="3:hp1400">
<ns1:IndictmentXML algorithm="u192036979" indictmentType="Wrong Polarity">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:11.000+07:00" repairActionType="-" indictmentType="Wrong Polarity" comment="-" repairStatus="False Call"/>
<ns1:ComponentXML packageId="192036979" partId="192036979" designator="3:hp1400"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="3:d506">
<ns1:IndictmentXML algorithm="u192027714" indictmentType="Wrong Polarity">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:11.000+07:00" repairActionType="-" indictmentType="Wrong Polarity" comment="-" repairStatus="False Call"/>
<ns1:ComponentXML packageId="192027714" partId="192027714" designator="3:d506"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="3:j1201">
<ns1:IndictmentXML algorithm="u192030753" indictmentType="Skewed">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:17.000+07:00" repairActionType="-" indictmentType="Skewed" comment="-" repairStatus="Repaired"/>
<ns1:ComponentXML packageId="192030753" partId="192030753" designator="3:j1201"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="3:u2101">
<ns1:IndictmentXML algorithm="u192028597" indictmentType="Tombstoned">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:24.000+07:00" repairActionType="-" indictmentType="Tombstoned" comment="-" repairStatus="Repaired"/>
<ns1:ComponentXML packageId="192028597" partId="192028597" designator="3:u2101"/>
</ns1:IndictmentXML>
</ns1:TestXML>
</ns1:BoardTestXMLExport>
代码:
foreach (string FILE_PATH in Directory.EnumerateFiles(@"Test", "*.xml"))
{
//Create xml reader
XmlReader xmlFile = XmlReader.Create(FILE_PATH, new XmlReaderSettings());
DataSet dataSet = new DataSet();
//Read xml to dataset
dataSet.ReadXml(xmlFile);
//Pass empdetails table to datagridview datasource
dataGridView1.DataSource = dataSet.Tables["BoardTestXMLExport"];
//Close xml reader
xmlFile.Close();
xmlFile.Close();
}
尝试以下 xml linq :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string FOLDER = @"c:\temp\";
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("filename", typeof(string));
dt.Columns.Add("numberOfIndictedComponents", typeof(int));
dt.Columns.Add("testerTestStartTime", typeof(DateTime));
dt.Columns.Add("testTime", typeof(DateTime));
dt.Columns.Add("repairStationId", typeof(string));
dt.Columns.Add("testStatus", typeof(string));
dt.Columns.Add("testerTestEndTime", typeof(DateTime));
dt.Columns.Add("imageId", typeof(string));
dt.Columns.Add("serialNumber", typeof(string));
dt.Columns.Add("assemblyRevision", typeof(string));
dt.Columns.Add("boardType", typeof(string));
dt.Columns.Add("boardRevision", typeof(string));
dt.Columns.Add("testerName", typeof(string));
dt.Columns.Add("stage", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("algorithm", typeof(string));
dt.Columns.Add("indictmentType", typeof(string));
dt.Columns.Add("repairOperator", typeof(string));
dt.Columns.Add("repairTime", typeof(DateTime));
dt.Columns.Add("repairActionType", typeof(string));
dt.Columns.Add("comment", typeof(string));
dt.Columns.Add("repairStatus", typeof(string));
dt.Columns.Add("packageId", typeof(string));
dt.Columns.Add("partId", typeof(string));
dt.Columns.Add("designator", typeof(string));
string[] filenames = Directory.GetFiles(FOLDER,"*.xml");
foreach (string filename in filenames)
{
XDocument doc = XDocument.Load(filename);
XElement BoardTestXMLExport = doc.Root;
XNamespace ns1 = BoardTestXMLExport.GetNamespaceOfPrefix("ns1");
int numberOfIndictedComponents = (int)BoardTestXMLExport.Attribute("numberOfIndictedComponents");
DateTime testerTestStartTime = (DateTime)BoardTestXMLExport.Attribute("testerTestStartTime");
DateTime testTime = (DateTime)BoardTestXMLExport.Attribute("testTime");
string repairStationId = (string)BoardTestXMLExport.Attribute("repairStationId");
string testStatus = (string)BoardTestXMLExport.Attribute("testStatus");
DateTime testerTestEndTime = (DateTime)BoardTestXMLExport.Attribute("testerTestEndTime");
XElement BoardXML = BoardTestXMLExport.Element(ns1 + "BoardXML");
int imageId = (int)BoardXML.Attribute("imageId");
string serialNumber = (string)BoardXML.Attribute("serialNumber");
string assemblyRevision = (string)BoardXML.Attribute("assemblyRevision");
string boardType = (string)BoardXML.Attribute("boardType");
string boardRevision = (string)BoardXML.Attribute("boardRevision");
XElement StationXML = BoardTestXMLExport.Element(ns1 + "StationXML");
string testerName = (string)StationXML.Attribute("testerName");
string stage = (string)StationXML.Attribute("stage");
foreach (XElement TestXML in doc.Descendants(ns1 + "TestXML"))
{
DataRow newRow = dt.Rows.Add();
newRow["filename"] = filename;
newRow["numberOfIndictedComponents"] = numberOfIndictedComponents;
newRow["testerTestStartTime"] = testerTestStartTime;
newRow["testTime"] = testTime;
newRow["repairStationId"] = repairStationId;
newRow["testStatus"] = testStatus;
newRow["testerTestEndTime"] = testerTestEndTime;
newRow["imageId"] = imageId;
newRow["serialNumber"] = serialNumber;
newRow["assemblyRevision"] = assemblyRevision;
newRow["boardType"] = boardType;
newRow["boardRevision"] = boardRevision;
newRow["boardType"] = boardType;
newRow["testerName"] = testerName;
newRow["stage"] = stage;
newRow["name"] = (string)TestXML.Attribute("name");
XElement IndictmentXML = TestXML.Element(ns1 + "IndictmentXML");
newRow["algorithm"] = (string)IndictmentXML.Attribute("algorithm");
newRow["indictmentType"] = (string)IndictmentXML.Attribute("indictmentType");
XElement RepairActionXML = TestXML.Descendants(ns1 + "RepairActionXML").FirstOrDefault();
newRow["repairOperator"] = (string)RepairActionXML.Attribute("repairOperator");
newRow["repairTime"] = (DateTime)RepairActionXML.Attribute("repairTime");
newRow["repairActionType"] = (string)RepairActionXML.Attribute("repairActionType");
newRow["comment"] = (string)RepairActionXML.Attribute("comment");
newRow["repairStatus"] = (string)RepairActionXML.Attribute("repairStatus");
XElement ComponentXML = TestXML.Descendants(ns1 + "ComponentXML").FirstOrDefault();
newRow["packageId"] = (string)ComponentXML.Attribute("packageId");
newRow["partId"] = (string)ComponentXML.Attribute("partId");
newRow["designator"] = (string)ComponentXML.Attribute("designator");
}
}
dataGridView1.DataSource = dt;
}
}
}
所以我有一个包含许多 XML 文件的目录。我试图在 datagirdview 中显示数据集,但内容不足,请有人帮我显示文件中的所有内容 下面是我使用的代码和XML。
<?xml version="1.0" encoding="UTF-8"?>
<ns1:BoardTestXMLExport numberOfIndictedComponents="4" testerTestStartTime="2021-01-18T07:46:32.000+07:00" testTime="2021-01-18T07:46:24.000+07:00" repairStationId="VNHCMING100" testStatus="Repaired" testerTestEndTime="2021-01-18T07:46:37.000+07:00" xmlns:ns1="http://tempuri.org/BoardTestXMLExport.xsd" numberOfIndictedPins="0" numberOfComponentsTested="320" numberOfJointsTested="0" numberOfDefects="4" repairStatus="Repaired">
<ns1:BoardXML imageId="3" serialNumber="21017227600" assemblyRevision="ING-296269012AC-A-B" boardType="ING-296269012AC-A-B" boardRevision="1610927415000"/>
<ns1:StationXML testerName="HCMINGAOI02" stage="V510"/>
<ns1:RepairEventXML numberOfVariationOkDefects="0" numberOfFalseCalledPins="0" numberOfRepairedComponents="2" numberOfVariationOkPins="0" numberOfRepairedPins="0" numberOfRepairLaterPins="0" numberOfFalseCalledDefects="2" numberOfActiveDefects="0" numberOfVariationOkComponents="0" repairEndTime="2021-01-18T07:49:24.000+07:00" repairStartTime="2021-01-18T07:49:10.000+07:00" numberOfRepairLaterDefects="0" repairOperator="c_admin" numberOfRepairLaterComponents="0" numberOfActiveComponents="0" numberOfActivePins="0" numberOfRepairedDefects="2" numberOfFalseCalledComponents="2"/>
<ns1:TestXML name="3:hp1400">
<ns1:IndictmentXML algorithm="u192036979" indictmentType="Wrong Polarity">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:11.000+07:00" repairActionType="-" indictmentType="Wrong Polarity" comment="-" repairStatus="False Call"/>
<ns1:ComponentXML packageId="192036979" partId="192036979" designator="3:hp1400"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="3:d506">
<ns1:IndictmentXML algorithm="u192027714" indictmentType="Wrong Polarity">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:11.000+07:00" repairActionType="-" indictmentType="Wrong Polarity" comment="-" repairStatus="False Call"/>
<ns1:ComponentXML packageId="192027714" partId="192027714" designator="3:d506"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="3:j1201">
<ns1:IndictmentXML algorithm="u192030753" indictmentType="Skewed">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:17.000+07:00" repairActionType="-" indictmentType="Skewed" comment="-" repairStatus="Repaired"/>
<ns1:ComponentXML packageId="192030753" partId="192030753" designator="3:j1201"/>
</ns1:IndictmentXML>
</ns1:TestXML>
<ns1:TestXML name="3:u2101">
<ns1:IndictmentXML algorithm="u192028597" indictmentType="Tombstoned">
<ns1:RepairActionXML repairOperator="c_admin" repairTime="2021-01-18T07:49:24.000+07:00" repairActionType="-" indictmentType="Tombstoned" comment="-" repairStatus="Repaired"/>
<ns1:ComponentXML packageId="192028597" partId="192028597" designator="3:u2101"/>
</ns1:IndictmentXML>
</ns1:TestXML>
</ns1:BoardTestXMLExport>
代码:
foreach (string FILE_PATH in Directory.EnumerateFiles(@"Test", "*.xml"))
{
//Create xml reader
XmlReader xmlFile = XmlReader.Create(FILE_PATH, new XmlReaderSettings());
DataSet dataSet = new DataSet();
//Read xml to dataset
dataSet.ReadXml(xmlFile);
//Pass empdetails table to datagridview datasource
dataGridView1.DataSource = dataSet.Tables["BoardTestXMLExport"];
//Close xml reader
xmlFile.Close();
xmlFile.Close();
}
尝试以下 xml linq :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string FOLDER = @"c:\temp\";
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("filename", typeof(string));
dt.Columns.Add("numberOfIndictedComponents", typeof(int));
dt.Columns.Add("testerTestStartTime", typeof(DateTime));
dt.Columns.Add("testTime", typeof(DateTime));
dt.Columns.Add("repairStationId", typeof(string));
dt.Columns.Add("testStatus", typeof(string));
dt.Columns.Add("testerTestEndTime", typeof(DateTime));
dt.Columns.Add("imageId", typeof(string));
dt.Columns.Add("serialNumber", typeof(string));
dt.Columns.Add("assemblyRevision", typeof(string));
dt.Columns.Add("boardType", typeof(string));
dt.Columns.Add("boardRevision", typeof(string));
dt.Columns.Add("testerName", typeof(string));
dt.Columns.Add("stage", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("algorithm", typeof(string));
dt.Columns.Add("indictmentType", typeof(string));
dt.Columns.Add("repairOperator", typeof(string));
dt.Columns.Add("repairTime", typeof(DateTime));
dt.Columns.Add("repairActionType", typeof(string));
dt.Columns.Add("comment", typeof(string));
dt.Columns.Add("repairStatus", typeof(string));
dt.Columns.Add("packageId", typeof(string));
dt.Columns.Add("partId", typeof(string));
dt.Columns.Add("designator", typeof(string));
string[] filenames = Directory.GetFiles(FOLDER,"*.xml");
foreach (string filename in filenames)
{
XDocument doc = XDocument.Load(filename);
XElement BoardTestXMLExport = doc.Root;
XNamespace ns1 = BoardTestXMLExport.GetNamespaceOfPrefix("ns1");
int numberOfIndictedComponents = (int)BoardTestXMLExport.Attribute("numberOfIndictedComponents");
DateTime testerTestStartTime = (DateTime)BoardTestXMLExport.Attribute("testerTestStartTime");
DateTime testTime = (DateTime)BoardTestXMLExport.Attribute("testTime");
string repairStationId = (string)BoardTestXMLExport.Attribute("repairStationId");
string testStatus = (string)BoardTestXMLExport.Attribute("testStatus");
DateTime testerTestEndTime = (DateTime)BoardTestXMLExport.Attribute("testerTestEndTime");
XElement BoardXML = BoardTestXMLExport.Element(ns1 + "BoardXML");
int imageId = (int)BoardXML.Attribute("imageId");
string serialNumber = (string)BoardXML.Attribute("serialNumber");
string assemblyRevision = (string)BoardXML.Attribute("assemblyRevision");
string boardType = (string)BoardXML.Attribute("boardType");
string boardRevision = (string)BoardXML.Attribute("boardRevision");
XElement StationXML = BoardTestXMLExport.Element(ns1 + "StationXML");
string testerName = (string)StationXML.Attribute("testerName");
string stage = (string)StationXML.Attribute("stage");
foreach (XElement TestXML in doc.Descendants(ns1 + "TestXML"))
{
DataRow newRow = dt.Rows.Add();
newRow["filename"] = filename;
newRow["numberOfIndictedComponents"] = numberOfIndictedComponents;
newRow["testerTestStartTime"] = testerTestStartTime;
newRow["testTime"] = testTime;
newRow["repairStationId"] = repairStationId;
newRow["testStatus"] = testStatus;
newRow["testerTestEndTime"] = testerTestEndTime;
newRow["imageId"] = imageId;
newRow["serialNumber"] = serialNumber;
newRow["assemblyRevision"] = assemblyRevision;
newRow["boardType"] = boardType;
newRow["boardRevision"] = boardRevision;
newRow["boardType"] = boardType;
newRow["testerName"] = testerName;
newRow["stage"] = stage;
newRow["name"] = (string)TestXML.Attribute("name");
XElement IndictmentXML = TestXML.Element(ns1 + "IndictmentXML");
newRow["algorithm"] = (string)IndictmentXML.Attribute("algorithm");
newRow["indictmentType"] = (string)IndictmentXML.Attribute("indictmentType");
XElement RepairActionXML = TestXML.Descendants(ns1 + "RepairActionXML").FirstOrDefault();
newRow["repairOperator"] = (string)RepairActionXML.Attribute("repairOperator");
newRow["repairTime"] = (DateTime)RepairActionXML.Attribute("repairTime");
newRow["repairActionType"] = (string)RepairActionXML.Attribute("repairActionType");
newRow["comment"] = (string)RepairActionXML.Attribute("comment");
newRow["repairStatus"] = (string)RepairActionXML.Attribute("repairStatus");
XElement ComponentXML = TestXML.Descendants(ns1 + "ComponentXML").FirstOrDefault();
newRow["packageId"] = (string)ComponentXML.Attribute("packageId");
newRow["partId"] = (string)ComponentXML.Attribute("partId");
newRow["designator"] = (string)ComponentXML.Attribute("designator");
}
}
dataGridView1.DataSource = dt;
}
}
}