XML 文件能够读取,但我一直在尝试使 class 正确,以便我可以在 DataGridView1 中显示选定的元素
XML file is able to read but i am stuck trying to get the class correct so I can display selected Elements in DataGridView1
我正在创建一个新程序,它将从多个 Xml 文件中获取信息,并比较如果超出某些参数则返回错误的数据。该程序运行没有错误,但我不知道如何将来自某些元素和属性的信息显示到我的 dataGridView 中。我可能更愿意将每个元素或属性写入单独的文本框。
这是目前适用于我的其他 XML 文件的代码,但是这个文件的属性和后代略有不同,我需要从 TestResult/Data/mList Val= 中获取 Val="" 属性""
DataTable dt10 = new DataTable();
dt10.Columns.Add("model", typeof(string));
dt10.Columns.Add("serial number", typeof(string));
dt10.Columns.Add("fw version", typeof(string));
dt10.Columns.Add("system", typeof(string));
dt10.Columns.Add("test plan", typeof(string));
dt10.Columns.Add("test date", typeof(DateTime));
dt10.Columns.Add("test start time", typeof(DateTime));
XDocument docrtnref = XDocument.Load(ofdRTNREF.FileName);
XElement root = docrtnref.Root;
XElement header = root.Element("Header");
XElement testdata = root.Element("TestData");
string model = (string)header.Element("model");
string sn = (string)header.Element("serialNumber");
string fwVersion = (string)header.Element("fwVersion");
string system = (string)header.Element("system");
string testPlan = (string)header.Element("testPlan");
DateTime testDate = (DateTime)header.Element("testDate");
TimeSpan testTime = DateTime.ParseExact((string)header.Element("testTime"), "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).TimeOfDay;
testDate = testDate.Add(testTime);
DateTime testStartTime = (DateTime)header.Element("testStartTime");
dt10.Rows.Add(new object[] {
model, sn, fwVersion, system,testPlan, testDate, testStartTime,
});
dataGridView10.DataSource = dt10;
DataTable dt9 = new DataTable();
dt9.Columns.Add("Type", typeof(string));
dt9.Columns.Add("cID", typeof(int));
dt9.Columns.Add("mID", typeof(decimal));
XElement data = root.Descendants("Data").FirstOrDefault();
foreach (XElement ch in data.Elements("CH")) //No longer in this xml
{
int cID = (int)ch.Attribute("cID"); //No longer in this xml
foreach (XElement meas in ch.Descendants("Meas"))//this would now be data.Descendants
{
decimal? val = (string)meas.Attribute("Val") == string.Empty ? null : (decimal?)meas.Attribute("Val");
dt9.Rows.Add(new object[] { "Meas", cID, val });
}
XElement subData = ch.Descendants("Data").FirstOrDefault();
if (subData != null)
{
foreach (XElement mList in subData.Descendants("mList"))
{
string val = (string)mList.Attribute("Val");
decimal? numVal = null;
switch (val)
{
case "True":
numVal = 1;
break;
case "False":
numVal = 0;
break;
default:
numVal = val == null ? null : val == string.Empty ? null : (decimal?)decimal.Parse(val);
break;
}
dt9.Rows.Add(new object[] { "Mlist", cID, numVal });
}
}
}
dataGridView9.DataSource = dt9;
在 YouTube 视频的其他视频中,这会在 DataGridView1 上显示新表格,但我没有任何反应,没有错误只是选择了 Xml,然后什么也没有。
这是 Xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<SunriseDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TestData>
<type>Sweep</type>
<subTest>
<SWEEP>
<Markers>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="0" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="98000000" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="0" />
</Summary>
</SavedSweepMarker>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="0" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="743000000" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="0" />
</Summary>
</SavedSweepMarker>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="1" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="0" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="2" />
</Summary>
</SavedSweepMarker>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="1" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="0" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="-2" />
</Summary>
</SavedSweepMarker>
</Markers>
<GuardTable>
<Data>
<mList mID="10220" lID="1999" Unit="4" Sto="8" Val="83000000" />
<mList mID="10221" lID="1999" Unit="4" Sto="8" Val="873000000" />
<mList mID="10223" lID="1999" Unit="0" Sto="8" Val="96" />
<mList mID="10222" lID="1999" Unit="9" Sto="8" Val="0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0" />
<mList mID="10224" lID="1999" Unit="4" Sto="8" Val="87000000 93000000 98000000 103000000 111000000 119000000 125500000 132000000 140000000 152000000 160000000 168000000 175500000 183000000 191000000 199000000 207000000 215000000 223000000 231000000 239000000 247000000 255000000 263000000 271000000 279000000 287000000 295000000 303000000 311000000 319000000 327000000 335000000 343000000 351000000 359000000 367000000 375000000 383000000 391000000 399000000 407000000 415000000 427250000 432000000 438000000 446000000 454000000 462000000 470000000 478000000 486000000 494000000 510000000 534000000 557000000 571000000 578000000 586000000 594000000 602000000 610000000 618000000 626000000 634000000 642000000 650000000 658000000 666000000 674000000 682000000 690000000 698000000 706000000 714000000 722000000 730000000 738000000 743000000 747000000 754000000 762000000 770000000 778000000 786000000 794000000 802000000 810000000 818000000 826000000 834000000 842000000 850000000 858000000 863500000 867500000" />
<mList mID="10225" lID="1999" Unit="4" Sto="8" Val="2000000 4000000 1000000 4000000 4000000 4000000 2500000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 1000000 500000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 12000000 12000000 11000000 3000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 1000000 3000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 1500000 2500000" />
</Data>
</GuardTable>
<TestResult>
<Data>
<mList mID="10206" lID="1999" Unit="0" Sto="8" Val="1" />
<mList mID="10212" lID="1701" Unit="2" Sto="8" Val="8.03" />
<mList mID="10213" lID="1702" Unit="2" Sto="8" Val="18.69" />
<mList mID="10202" lID="1703" Unit="1" Sto="8" Val="10.61" />
<mList mID="10203" lID="1704" Unit="1" Sto="8" Val="15.05355" />
<mList mID="10207" lID="1999" Unit="0" Sto="8" Val="401" />
<mList mID="10201" lID="1999" Unit="4" Sto="8" Val="83000000 85000000 87000000 89000000 93000000 93000000 97000000 98000000 98000000 103000000 103000000 107000000 111000000 111000000 111000000 115000000 119000000 119000000 119000000 123000000 125500000 125500000 125500000 132000000 132000000 132000000 136000000 140000000 140000000 140000000 144000000 144220000 146200000 148000000 152000000 152000000 156000000 160000000 160000000 160000000 164000000 168000000 168000000 168000000 168000000 168000000 175500000 175500000 175500000 175500000 183000000 183000000 187000000 191000000 191000000 191000000 195000000 199000000 199000000 199000000 203000000 207000000 207000000 207000000 211000000 215000000 215000000 215000000 219000000 223000000 223000000 223000000 227000000 231000000 231000000 231000000 235000000 239000000 239000000 239000000 243000000 247000000 247000000 247000000 251000000 255000000 255000000 255000000 259000000 263000000 263000000 263000000 267000000 271000000 271000000 271000000 275000000 279000000 279000000 279000000 283000000 287000000 287000000 287000000 291000000 295000000 295000000 295000000 299000000 303000000 303000000 303000000 307000000 311000000 311000000 311000000 315000000 319000000 319000000 319000000 319000000 323000000 327000000 327000000 327000000 331000000 335000000 335000000 335000000 339000000 343000000 343000000 343000000 347000000 351000000 351000000 351000000 355000000 359000000 359000000 359000000 363000000 367000000 367000000 367000000 371000000 375000000 375000000 375000000 379000000 383000000 383000000 383000000 387000000 391000000 391000000 391000000 395000000 399000000 399000000 399000000 403000000 407000000 407000000 407000000 411000000 415000000 415000000 415000000 415000000 419000000 420720000 422700000 424670000 427250000 428620000 431500000 432000000 438000000 438000000 438000000 442000000 446000000 446000000 446000000 450000000 454000000 454000000 454000000 458000000 462000000 462000000 462000000 466000000 470000000 470000000 470000000 474000000 478000000 478000000 478000000 482000000 486000000 486000000 486000000 490000000 494000000 494000000 494000000 498000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 522000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 546000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 568000000 571000000 571000000 574000000 578000000 578000000 578000000 582000000 586000000 586000000 586000000 590000000 594000000 594000000 594000000 598000000 602000000 602000000 602000000 606000000 610000000 610000000 610000000 614000000 618000000 618000000 618000000 622000000 626000000 626000000 626000000 630000000 634000000 634000000 634000000 638000000 642000000 642000000 642000000 646000000 650000000 650000000 650000000 654000000 658000000 658000000 658000000 662000000 666000000 666000000 666000000 670000000 674000000 674000000 674000000 678000000 682000000 682000000 682000000 686000000 690000000 690000000 690000000 694000000 698000000 698000000 698000000 702000000 706000000 706000000 706000000 710000000 714000000 714000000 714000000 714000000 718000000 722000000 722000000 722000000 726000000 730000000 730000000 730000000 734000000 738000000 738000000 738000000 742000000 743000000 747000000 747000000 750000000 754000000 754000000 754000000 758000000 762000000 762000000 762000000 766000000 770000000 770000000 770000000 774000000 778000000 778000000 778000000 782000000 786000000 786000000 786000000 790000000 794000000 794000000 794000000 798000000 802000000 802000000 802000000 806000000 810000000 810000000 810000000 814000000 818000000 818000000 818000000 822000000 826000000 826000000 826000000 830000000 834000000 834000000 834000000 838000000 842000000 842000000 842000000 846000000 850000000 850000000 850000000 854000000 858000000 858000000 858000000 862000000 863500000 863500000 867500000 867500000 870000000 871020000 873000000 98000000 743000000" />
<mList mID="10200" lID="1999" Unit="1" Sto="8" Val="-6.46 -6.28 -5.45 -5.45 -4.87 -4.87 -4.87 8.03 8.03 -4.94 -4.94 -4.94 -4.51 -4.51 -4.51 -4.51 -4.91 -4.91 -4.91 -4.91 -3.45 -3.45 -3.45 -3.45 -3.45 -3.45 -3.45 -3.56 -3.56 -3.56 -3.56 -4.3 -3.88 -4.17 -3.77 -3.77 -3.77 -4.23 -4.23 -4.23 -4.23 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.63 -3.63 -3.63 -3.63 -3.71 -3.71 -3.71 -3.71 -3.77 -3.77 -3.77 -3.77 -3.87 -3.87 -3.87 -3.87 -3.73 -3.73 -3.73 -3.73 -3.7 -3.7 -3.7 -3.7 -2.67 -2.67 -2.67 -2.67 -2.96 -2.96 -2.96 -2.96 -2.79 -2.79 -2.79 -2.79 -2.61 -2.61 -2.61 -2.61 -0.75 -0.75 -0.75 -0.75 -3.07 -3.07 -3.07 -3.07 -2.71 -2.71 -2.71 -2.71 -2.4 -2.4 -2.4 -2.4 -2.26 -2.26 -2.26 -2.26 -1.8 -1.8 -1.8 -1.8 -0.86 -0.86 -0.86 -0.86 -0.86 -1.43 -1.43 -1.43 -1.43 -1.56 -1.56 -1.56 -1.56 -1.11 -1.11 -1.11 -1.11 -0.58 -0.58 -0.58 -0.58 3.16 3.16 3.16 3.16 -1.4 -1.4 -1.4 -1.4 0.77 0.77 0.77 0.77 -0.39 -0.39 -0.39 -0.39 -0.8 -0.8 -0.8 -0.8 2.05 2.05 2.05 2.05 1.26 1.26 1.26 1.26 1.37 1.37 1.37 1.37 1.37 1.39 11.26 1.45 1.29 1.29 1.69 5.93 5.93 5.93 5.93 5.93 3.59 3.59 3.59 3.59 3.74 3.74 3.74 3.74 4.03 4.03 4.03 4.03 1.35 1.35 1.35 1.35 2.1 2.1 2.1 2.1 3.18 3.18 3.18 3.18 2.7 2.7 2.7 2.7 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.12 4.12 4.12 5.72 5.72 5.72 5.72 5.24 5.24 5.24 5.24 6.63 6.63 6.63 6.63 7.52 7.52 7.52 7.52 3.79 3.79 3.79 3.79 7.45 7.45 7.45 7.45 7.42 7.42 7.42 7.42 5.79 5.79 5.79 5.79 6.29 6.29 6.29 6.29 5.91 5.91 5.91 5.91 6.28 6.28 6.28 6.28 6.77 6.77 6.77 6.77 6.44 6.44 6.44 6.44 6.1 6.1 6.1 6.1 7.48 7.48 7.48 7.48 6.63 6.63 6.63 6.63 7.6 7.6 7.6 7.6 6.52 6.52 6.52 6.52 6.52 5.07 5.07 5.07 5.07 4.9 4.9 4.9 4.9 3.57 3.57 3.57 3.57 18.64 2.33 2.33 2.33 3.75 3.75 3.75 3.75 2.96 2.96 2.96 2.96 4.56 4.56 4.56 4.56 4.59 4.59 4.59 4.59 3.98 3.98 3.98 3.98 3.17 3.17 3.17 3.17 3.82 3.82 3.82 3.82 3.62 3.62 3.62 3.62 2.92 2.92 2.92 2.92 3.42 3.42 3.42 3.42 2.63 2.63 2.63 2.63 3.7 3.7 3.7 3.7 3 3 3 3 2 2 2 2 17.74 17.74 -0.45 -0.45 -0.45 -0.38 -0.38 8.03 18.69" />
<mList mID="10208" lID="1999" Unit="2" Sto="8" Val="3700" />
<mList mID="10209" lID="1999" Unit="1" Sto="8" Val="0" />
<mList mID="10210" lID="1999" Unit="1" Sto="8" Val="" />
<mList mID="10211" lID="1999" Unit="0" Sto="8" />
<mList mID="10204" lID="1999" Unit="4" Sto="8" Val="98000000" />
<mList mID="10205" lID="1999" Unit="4" Sto="8" Val="743000000" />
<mList mID="10214" lID="1999" Unit="1" Sto="8" Val="0" />
<mList mID="10215" lID="1999" Unit="1" Sto="8" Val="0" />
<mList mID="10216" lID="1999" Unit="0" Sto="8" Val="0" />
<mList mID="10217" lID="1999" Unit="0" Sto="8" Val="0" />
<mList mID="10218" lID="1999" Unit="0" Sto="8" Val="0" />
</Data>
</TestResult>
</SWEEP>
</subTest>
</TestData>
</SunriseDocument>
我使用 xml linq 并将数据放入数据表中,这使得将结果放入 DGV 变得非常容易
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string FILENAME = @"c:\temp\test.xml";
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("device", typeof(string));
dt.Columns.Add("model", typeof(string));
dt.Columns.Add("serial number", typeof(string));
dt.Columns.Add("cal site", typeof(string));
dt.Columns.Add("cal date", typeof(DateTime));
dt.Columns.Add("unit", typeof(string));
dt.Columns.Add("temperature", typeof(int));
dt.Columns.Add("fw version", typeof(string));
dt.Columns.Add("system", typeof(string));
dt.Columns.Add("channel", typeof(string));
dt.Columns.Add("test plan", typeof(string));
dt.Columns.Add("user", typeof(string));
dt.Columns.Add("site", typeof(string));
dt.Columns.Add("site address", typeof(string));
dt.Columns.Add("work order", typeof(string));
dt.Columns.Add("guid", typeof(string));
dt.Columns.Add("comment", typeof(string));
dt.Columns.Add("test date", typeof(DateTime));
dt.Columns.Add("test start time", typeof(DateTime));
dt.Columns.Add("test series", typeof(string));
dt.Columns.Add("status", typeof(int));
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XElement header = root.Element("Header");
if (header != null)
{
string device = (string)header.Element("device");
string model = (string)header.Element("model");
string sn = (string)header.Element("serialNumber");
string calSite = (string)header.Element("calSite");
DateTime calDate = (DateTime)header.Element("calDate");
string unit = (string)header.Element("temperature").Element("unit");
int temperature = (int)header.Element("temperature").Element("value");
string fwVersion = (string)header.Element("fwVersion");
string system = (string)header.Element("system");
string channel = (string)header.Element("channel");
string testPlan = (string)header.Element("testPlan");
string user = (string)header.Element("user");
string site = (string)header.Element("site");
string siteAddr = (string)header.Element("siteAddr");
string workOrder = (string)header.Element("workOrder");
string guid = (string)header.Element("guid");
string comment = (string)header.Element("comment");
DateTime testDate = (DateTime)header.Element("testDate");
TimeSpan testTime = DateTime.ParseExact((string)header.Element("testTime"), "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).TimeOfDay;
testDate = testDate.Add(testTime);
DateTime testStartTime = (DateTime)header.Element("testStartTime");
string testSeries = (string)header.Element("testSeries");
int status = (int)header.Element("status");
dt.Rows.Add(new object[] {
device, model, sn, calSite, calDate, unit, temperature, fwVersion, system,
channel, testPlan, user, site, siteAddr, workOrder, guid, comment, testDate, testStartTime, testSeries, status
});
dataGridView1.DataSource = dt;
}
XElement testData = root.Element("TestData");
string xType = (string)testData.Element("type");
DataTable dt2 = new DataTable();
XElement data = testData.Descendants("Data").FirstOrDefault();
switch (xType)
{
case "AutoTest" :
dt2.Columns.Add("Type", typeof(string));
dt2.Columns.Add("cID", typeof(int));
dt2.Columns.Add("mID", typeof(int));
dt2.Columns.Add("lID", typeof(int));
dt2.Columns.Add("Unit", typeof(int));
dt2.Columns.Add("Sto", typeof(int));
dt2.Columns.Add("Val", typeof(decimal));
foreach (XElement ch in data.Elements("CH"))
{
int cID = (int)ch.Attribute("cID");
foreach (XElement meas in ch.Descendants("Meas"))
{
int mID = (int)meas.Attribute("mID");
int lID = (int)meas.Attribute("lID");
int dataUnit = (int)meas.Attribute("Unit");
int sto = (int)meas.Attribute("Sto");
decimal? val = (string)meas.Attribute("Val") == string.Empty ? null : (decimal?)meas.Attribute("Val");
dt2.Rows.Add(new object[] { "Meas", cID, mID, lID, dataUnit, sto, val });
}
XElement subData = ch.Descendants("Data").FirstOrDefault();
if (subData != null)
{
foreach (XElement mList in subData.Descendants("mList"))
{
int mID = (int)mList.Attribute("mID");
int lID = (int)mList.Attribute("lID");
int dataUnit = (int)mList.Attribute("Unit");
int sto = (int)mList.Attribute("Sto");
string val = (string)mList.Attribute("Val");
decimal? numVal = null;
switch (val)
{
case "True":
numVal = 1;
break;
case "False":
numVal = 0;
break;
default:
numVal = val == null ? null : val == string.Empty ? null : (decimal?)decimal.Parse(val);
break;
}
dt2.Rows.Add(new object[] { "Mlist", cID, mID, lID, dataUnit, sto, numVal });
}
}
}
break;
case "Sweep" :
dt2.Columns.Add("Type", typeof(string));
dt2.Columns.Add("mID", typeof(int));
dt2.Columns.Add("lID", typeof(int));
dt2.Columns.Add("Unit", typeof(int));
dt2.Columns.Add("Sto", typeof(int));
dt2.Columns.Add("Val", typeof(string));
XElement testResults = testData.Descendants("TestResult").FirstOrDefault();
foreach (XElement mList in testResults.Descendants("mList"))
{
int mID = (int)mList.Attribute("mID");
int lID = (int)mList.Attribute("lID");
int dataUnit = (int)mList.Attribute("Unit");
int sto = (int)mList.Attribute("Sto");
string val = (string)mList.Attribute("Val");
dt2.Rows.Add(new object[] { "Mlist", mID, lID, dataUnit, sto, val });
}
break;
}
dataGridView2.DataSource = dt2;
}
}
}
我正在创建一个新程序,它将从多个 Xml 文件中获取信息,并比较如果超出某些参数则返回错误的数据。该程序运行没有错误,但我不知道如何将来自某些元素和属性的信息显示到我的 dataGridView 中。我可能更愿意将每个元素或属性写入单独的文本框。
这是目前适用于我的其他 XML 文件的代码,但是这个文件的属性和后代略有不同,我需要从 TestResult/Data/mList Val= 中获取 Val="" 属性""
DataTable dt10 = new DataTable();
dt10.Columns.Add("model", typeof(string));
dt10.Columns.Add("serial number", typeof(string));
dt10.Columns.Add("fw version", typeof(string));
dt10.Columns.Add("system", typeof(string));
dt10.Columns.Add("test plan", typeof(string));
dt10.Columns.Add("test date", typeof(DateTime));
dt10.Columns.Add("test start time", typeof(DateTime));
XDocument docrtnref = XDocument.Load(ofdRTNREF.FileName);
XElement root = docrtnref.Root;
XElement header = root.Element("Header");
XElement testdata = root.Element("TestData");
string model = (string)header.Element("model");
string sn = (string)header.Element("serialNumber");
string fwVersion = (string)header.Element("fwVersion");
string system = (string)header.Element("system");
string testPlan = (string)header.Element("testPlan");
DateTime testDate = (DateTime)header.Element("testDate");
TimeSpan testTime = DateTime.ParseExact((string)header.Element("testTime"), "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).TimeOfDay;
testDate = testDate.Add(testTime);
DateTime testStartTime = (DateTime)header.Element("testStartTime");
dt10.Rows.Add(new object[] {
model, sn, fwVersion, system,testPlan, testDate, testStartTime,
});
dataGridView10.DataSource = dt10;
DataTable dt9 = new DataTable();
dt9.Columns.Add("Type", typeof(string));
dt9.Columns.Add("cID", typeof(int));
dt9.Columns.Add("mID", typeof(decimal));
XElement data = root.Descendants("Data").FirstOrDefault();
foreach (XElement ch in data.Elements("CH")) //No longer in this xml
{
int cID = (int)ch.Attribute("cID"); //No longer in this xml
foreach (XElement meas in ch.Descendants("Meas"))//this would now be data.Descendants
{
decimal? val = (string)meas.Attribute("Val") == string.Empty ? null : (decimal?)meas.Attribute("Val");
dt9.Rows.Add(new object[] { "Meas", cID, val });
}
XElement subData = ch.Descendants("Data").FirstOrDefault();
if (subData != null)
{
foreach (XElement mList in subData.Descendants("mList"))
{
string val = (string)mList.Attribute("Val");
decimal? numVal = null;
switch (val)
{
case "True":
numVal = 1;
break;
case "False":
numVal = 0;
break;
default:
numVal = val == null ? null : val == string.Empty ? null : (decimal?)decimal.Parse(val);
break;
}
dt9.Rows.Add(new object[] { "Mlist", cID, numVal });
}
}
}
dataGridView9.DataSource = dt9;
在 YouTube 视频的其他视频中,这会在 DataGridView1 上显示新表格,但我没有任何反应,没有错误只是选择了 Xml,然后什么也没有。
这是 Xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<SunriseDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TestData>
<type>Sweep</type>
<subTest>
<SWEEP>
<Markers>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="0" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="98000000" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="0" />
</Summary>
</SavedSweepMarker>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="0" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="743000000" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="0" />
</Summary>
</SavedSweepMarker>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="1" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="0" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="2" />
</Summary>
</SavedSweepMarker>
<SavedSweepMarker>
<Summary>
<Setup mID="10240" lID="1999" Unit="0" Sto="4" Val="1" />
<Setup mID="10241" lID="1999" Unit="4" Sto="5" Val="0" />
<Setup mID="10242" lID="1999" Unit="2" Sto="7" Val="-2" />
</Summary>
</SavedSweepMarker>
</Markers>
<GuardTable>
<Data>
<mList mID="10220" lID="1999" Unit="4" Sto="8" Val="83000000" />
<mList mID="10221" lID="1999" Unit="4" Sto="8" Val="873000000" />
<mList mID="10223" lID="1999" Unit="0" Sto="8" Val="96" />
<mList mID="10222" lID="1999" Unit="9" Sto="8" Val="0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0" />
<mList mID="10224" lID="1999" Unit="4" Sto="8" Val="87000000 93000000 98000000 103000000 111000000 119000000 125500000 132000000 140000000 152000000 160000000 168000000 175500000 183000000 191000000 199000000 207000000 215000000 223000000 231000000 239000000 247000000 255000000 263000000 271000000 279000000 287000000 295000000 303000000 311000000 319000000 327000000 335000000 343000000 351000000 359000000 367000000 375000000 383000000 391000000 399000000 407000000 415000000 427250000 432000000 438000000 446000000 454000000 462000000 470000000 478000000 486000000 494000000 510000000 534000000 557000000 571000000 578000000 586000000 594000000 602000000 610000000 618000000 626000000 634000000 642000000 650000000 658000000 666000000 674000000 682000000 690000000 698000000 706000000 714000000 722000000 730000000 738000000 743000000 747000000 754000000 762000000 770000000 778000000 786000000 794000000 802000000 810000000 818000000 826000000 834000000 842000000 850000000 858000000 863500000 867500000" />
<mList mID="10225" lID="1999" Unit="4" Sto="8" Val="2000000 4000000 1000000 4000000 4000000 4000000 2500000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 1000000 500000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 12000000 12000000 11000000 3000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 1000000 3000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 4000000 1500000 2500000" />
</Data>
</GuardTable>
<TestResult>
<Data>
<mList mID="10206" lID="1999" Unit="0" Sto="8" Val="1" />
<mList mID="10212" lID="1701" Unit="2" Sto="8" Val="8.03" />
<mList mID="10213" lID="1702" Unit="2" Sto="8" Val="18.69" />
<mList mID="10202" lID="1703" Unit="1" Sto="8" Val="10.61" />
<mList mID="10203" lID="1704" Unit="1" Sto="8" Val="15.05355" />
<mList mID="10207" lID="1999" Unit="0" Sto="8" Val="401" />
<mList mID="10201" lID="1999" Unit="4" Sto="8" Val="83000000 85000000 87000000 89000000 93000000 93000000 97000000 98000000 98000000 103000000 103000000 107000000 111000000 111000000 111000000 115000000 119000000 119000000 119000000 123000000 125500000 125500000 125500000 132000000 132000000 132000000 136000000 140000000 140000000 140000000 144000000 144220000 146200000 148000000 152000000 152000000 156000000 160000000 160000000 160000000 164000000 168000000 168000000 168000000 168000000 168000000 175500000 175500000 175500000 175500000 183000000 183000000 187000000 191000000 191000000 191000000 195000000 199000000 199000000 199000000 203000000 207000000 207000000 207000000 211000000 215000000 215000000 215000000 219000000 223000000 223000000 223000000 227000000 231000000 231000000 231000000 235000000 239000000 239000000 239000000 243000000 247000000 247000000 247000000 251000000 255000000 255000000 255000000 259000000 263000000 263000000 263000000 267000000 271000000 271000000 271000000 275000000 279000000 279000000 279000000 283000000 287000000 287000000 287000000 291000000 295000000 295000000 295000000 299000000 303000000 303000000 303000000 307000000 311000000 311000000 311000000 315000000 319000000 319000000 319000000 319000000 323000000 327000000 327000000 327000000 331000000 335000000 335000000 335000000 339000000 343000000 343000000 343000000 347000000 351000000 351000000 351000000 355000000 359000000 359000000 359000000 363000000 367000000 367000000 367000000 371000000 375000000 375000000 375000000 379000000 383000000 383000000 383000000 387000000 391000000 391000000 391000000 395000000 399000000 399000000 399000000 403000000 407000000 407000000 407000000 411000000 415000000 415000000 415000000 415000000 419000000 420720000 422700000 424670000 427250000 428620000 431500000 432000000 438000000 438000000 438000000 442000000 446000000 446000000 446000000 450000000 454000000 454000000 454000000 458000000 462000000 462000000 462000000 466000000 470000000 470000000 470000000 474000000 478000000 478000000 478000000 482000000 486000000 486000000 486000000 490000000 494000000 494000000 494000000 498000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 510000000 522000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 534000000 546000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 557000000 568000000 571000000 571000000 574000000 578000000 578000000 578000000 582000000 586000000 586000000 586000000 590000000 594000000 594000000 594000000 598000000 602000000 602000000 602000000 606000000 610000000 610000000 610000000 614000000 618000000 618000000 618000000 622000000 626000000 626000000 626000000 630000000 634000000 634000000 634000000 638000000 642000000 642000000 642000000 646000000 650000000 650000000 650000000 654000000 658000000 658000000 658000000 662000000 666000000 666000000 666000000 670000000 674000000 674000000 674000000 678000000 682000000 682000000 682000000 686000000 690000000 690000000 690000000 694000000 698000000 698000000 698000000 702000000 706000000 706000000 706000000 710000000 714000000 714000000 714000000 714000000 718000000 722000000 722000000 722000000 726000000 730000000 730000000 730000000 734000000 738000000 738000000 738000000 742000000 743000000 747000000 747000000 750000000 754000000 754000000 754000000 758000000 762000000 762000000 762000000 766000000 770000000 770000000 770000000 774000000 778000000 778000000 778000000 782000000 786000000 786000000 786000000 790000000 794000000 794000000 794000000 798000000 802000000 802000000 802000000 806000000 810000000 810000000 810000000 814000000 818000000 818000000 818000000 822000000 826000000 826000000 826000000 830000000 834000000 834000000 834000000 838000000 842000000 842000000 842000000 846000000 850000000 850000000 850000000 854000000 858000000 858000000 858000000 862000000 863500000 863500000 867500000 867500000 870000000 871020000 873000000 98000000 743000000" />
<mList mID="10200" lID="1999" Unit="1" Sto="8" Val="-6.46 -6.28 -5.45 -5.45 -4.87 -4.87 -4.87 8.03 8.03 -4.94 -4.94 -4.94 -4.51 -4.51 -4.51 -4.51 -4.91 -4.91 -4.91 -4.91 -3.45 -3.45 -3.45 -3.45 -3.45 -3.45 -3.45 -3.56 -3.56 -3.56 -3.56 -4.3 -3.88 -4.17 -3.77 -3.77 -3.77 -4.23 -4.23 -4.23 -4.23 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.98 -3.63 -3.63 -3.63 -3.63 -3.71 -3.71 -3.71 -3.71 -3.77 -3.77 -3.77 -3.77 -3.87 -3.87 -3.87 -3.87 -3.73 -3.73 -3.73 -3.73 -3.7 -3.7 -3.7 -3.7 -2.67 -2.67 -2.67 -2.67 -2.96 -2.96 -2.96 -2.96 -2.79 -2.79 -2.79 -2.79 -2.61 -2.61 -2.61 -2.61 -0.75 -0.75 -0.75 -0.75 -3.07 -3.07 -3.07 -3.07 -2.71 -2.71 -2.71 -2.71 -2.4 -2.4 -2.4 -2.4 -2.26 -2.26 -2.26 -2.26 -1.8 -1.8 -1.8 -1.8 -0.86 -0.86 -0.86 -0.86 -0.86 -1.43 -1.43 -1.43 -1.43 -1.56 -1.56 -1.56 -1.56 -1.11 -1.11 -1.11 -1.11 -0.58 -0.58 -0.58 -0.58 3.16 3.16 3.16 3.16 -1.4 -1.4 -1.4 -1.4 0.77 0.77 0.77 0.77 -0.39 -0.39 -0.39 -0.39 -0.8 -0.8 -0.8 -0.8 2.05 2.05 2.05 2.05 1.26 1.26 1.26 1.26 1.37 1.37 1.37 1.37 1.37 1.39 11.26 1.45 1.29 1.29 1.69 5.93 5.93 5.93 5.93 5.93 3.59 3.59 3.59 3.59 3.74 3.74 3.74 3.74 4.03 4.03 4.03 4.03 1.35 1.35 1.35 1.35 2.1 2.1 2.1 2.1 3.18 3.18 3.18 3.18 2.7 2.7 2.7 2.7 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.14 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 5.77 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.73 4.12 4.12 4.12 5.72 5.72 5.72 5.72 5.24 5.24 5.24 5.24 6.63 6.63 6.63 6.63 7.52 7.52 7.52 7.52 3.79 3.79 3.79 3.79 7.45 7.45 7.45 7.45 7.42 7.42 7.42 7.42 5.79 5.79 5.79 5.79 6.29 6.29 6.29 6.29 5.91 5.91 5.91 5.91 6.28 6.28 6.28 6.28 6.77 6.77 6.77 6.77 6.44 6.44 6.44 6.44 6.1 6.1 6.1 6.1 7.48 7.48 7.48 7.48 6.63 6.63 6.63 6.63 7.6 7.6 7.6 7.6 6.52 6.52 6.52 6.52 6.52 5.07 5.07 5.07 5.07 4.9 4.9 4.9 4.9 3.57 3.57 3.57 3.57 18.64 2.33 2.33 2.33 3.75 3.75 3.75 3.75 2.96 2.96 2.96 2.96 4.56 4.56 4.56 4.56 4.59 4.59 4.59 4.59 3.98 3.98 3.98 3.98 3.17 3.17 3.17 3.17 3.82 3.82 3.82 3.82 3.62 3.62 3.62 3.62 2.92 2.92 2.92 2.92 3.42 3.42 3.42 3.42 2.63 2.63 2.63 2.63 3.7 3.7 3.7 3.7 3 3 3 3 2 2 2 2 17.74 17.74 -0.45 -0.45 -0.45 -0.38 -0.38 8.03 18.69" />
<mList mID="10208" lID="1999" Unit="2" Sto="8" Val="3700" />
<mList mID="10209" lID="1999" Unit="1" Sto="8" Val="0" />
<mList mID="10210" lID="1999" Unit="1" Sto="8" Val="" />
<mList mID="10211" lID="1999" Unit="0" Sto="8" />
<mList mID="10204" lID="1999" Unit="4" Sto="8" Val="98000000" />
<mList mID="10205" lID="1999" Unit="4" Sto="8" Val="743000000" />
<mList mID="10214" lID="1999" Unit="1" Sto="8" Val="0" />
<mList mID="10215" lID="1999" Unit="1" Sto="8" Val="0" />
<mList mID="10216" lID="1999" Unit="0" Sto="8" Val="0" />
<mList mID="10217" lID="1999" Unit="0" Sto="8" Val="0" />
<mList mID="10218" lID="1999" Unit="0" Sto="8" Val="0" />
</Data>
</TestResult>
</SWEEP>
</subTest>
</TestData>
</SunriseDocument>
我使用 xml linq 并将数据放入数据表中,这使得将结果放入 DGV 变得非常容易
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string FILENAME = @"c:\temp\test.xml";
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("device", typeof(string));
dt.Columns.Add("model", typeof(string));
dt.Columns.Add("serial number", typeof(string));
dt.Columns.Add("cal site", typeof(string));
dt.Columns.Add("cal date", typeof(DateTime));
dt.Columns.Add("unit", typeof(string));
dt.Columns.Add("temperature", typeof(int));
dt.Columns.Add("fw version", typeof(string));
dt.Columns.Add("system", typeof(string));
dt.Columns.Add("channel", typeof(string));
dt.Columns.Add("test plan", typeof(string));
dt.Columns.Add("user", typeof(string));
dt.Columns.Add("site", typeof(string));
dt.Columns.Add("site address", typeof(string));
dt.Columns.Add("work order", typeof(string));
dt.Columns.Add("guid", typeof(string));
dt.Columns.Add("comment", typeof(string));
dt.Columns.Add("test date", typeof(DateTime));
dt.Columns.Add("test start time", typeof(DateTime));
dt.Columns.Add("test series", typeof(string));
dt.Columns.Add("status", typeof(int));
XDocument doc = XDocument.Load(FILENAME);
XElement root = doc.Root;
XElement header = root.Element("Header");
if (header != null)
{
string device = (string)header.Element("device");
string model = (string)header.Element("model");
string sn = (string)header.Element("serialNumber");
string calSite = (string)header.Element("calSite");
DateTime calDate = (DateTime)header.Element("calDate");
string unit = (string)header.Element("temperature").Element("unit");
int temperature = (int)header.Element("temperature").Element("value");
string fwVersion = (string)header.Element("fwVersion");
string system = (string)header.Element("system");
string channel = (string)header.Element("channel");
string testPlan = (string)header.Element("testPlan");
string user = (string)header.Element("user");
string site = (string)header.Element("site");
string siteAddr = (string)header.Element("siteAddr");
string workOrder = (string)header.Element("workOrder");
string guid = (string)header.Element("guid");
string comment = (string)header.Element("comment");
DateTime testDate = (DateTime)header.Element("testDate");
TimeSpan testTime = DateTime.ParseExact((string)header.Element("testTime"), "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).TimeOfDay;
testDate = testDate.Add(testTime);
DateTime testStartTime = (DateTime)header.Element("testStartTime");
string testSeries = (string)header.Element("testSeries");
int status = (int)header.Element("status");
dt.Rows.Add(new object[] {
device, model, sn, calSite, calDate, unit, temperature, fwVersion, system,
channel, testPlan, user, site, siteAddr, workOrder, guid, comment, testDate, testStartTime, testSeries, status
});
dataGridView1.DataSource = dt;
}
XElement testData = root.Element("TestData");
string xType = (string)testData.Element("type");
DataTable dt2 = new DataTable();
XElement data = testData.Descendants("Data").FirstOrDefault();
switch (xType)
{
case "AutoTest" :
dt2.Columns.Add("Type", typeof(string));
dt2.Columns.Add("cID", typeof(int));
dt2.Columns.Add("mID", typeof(int));
dt2.Columns.Add("lID", typeof(int));
dt2.Columns.Add("Unit", typeof(int));
dt2.Columns.Add("Sto", typeof(int));
dt2.Columns.Add("Val", typeof(decimal));
foreach (XElement ch in data.Elements("CH"))
{
int cID = (int)ch.Attribute("cID");
foreach (XElement meas in ch.Descendants("Meas"))
{
int mID = (int)meas.Attribute("mID");
int lID = (int)meas.Attribute("lID");
int dataUnit = (int)meas.Attribute("Unit");
int sto = (int)meas.Attribute("Sto");
decimal? val = (string)meas.Attribute("Val") == string.Empty ? null : (decimal?)meas.Attribute("Val");
dt2.Rows.Add(new object[] { "Meas", cID, mID, lID, dataUnit, sto, val });
}
XElement subData = ch.Descendants("Data").FirstOrDefault();
if (subData != null)
{
foreach (XElement mList in subData.Descendants("mList"))
{
int mID = (int)mList.Attribute("mID");
int lID = (int)mList.Attribute("lID");
int dataUnit = (int)mList.Attribute("Unit");
int sto = (int)mList.Attribute("Sto");
string val = (string)mList.Attribute("Val");
decimal? numVal = null;
switch (val)
{
case "True":
numVal = 1;
break;
case "False":
numVal = 0;
break;
default:
numVal = val == null ? null : val == string.Empty ? null : (decimal?)decimal.Parse(val);
break;
}
dt2.Rows.Add(new object[] { "Mlist", cID, mID, lID, dataUnit, sto, numVal });
}
}
}
break;
case "Sweep" :
dt2.Columns.Add("Type", typeof(string));
dt2.Columns.Add("mID", typeof(int));
dt2.Columns.Add("lID", typeof(int));
dt2.Columns.Add("Unit", typeof(int));
dt2.Columns.Add("Sto", typeof(int));
dt2.Columns.Add("Val", typeof(string));
XElement testResults = testData.Descendants("TestResult").FirstOrDefault();
foreach (XElement mList in testResults.Descendants("mList"))
{
int mID = (int)mList.Attribute("mID");
int lID = (int)mList.Attribute("lID");
int dataUnit = (int)mList.Attribute("Unit");
int sto = (int)mList.Attribute("Sto");
string val = (string)mList.Attribute("Val");
dt2.Rows.Add(new object[] { "Mlist", mID, lID, dataUnit, sto, val });
}
break;
}
dataGridView2.DataSource = dt2;
}
}
}