我有一個現有的 XML 檔案,并且我有要在特定位置添加到此 xml 檔案中的資料。不幸的是,我無法在我想要的地方添加資料。我試圖做的是首先加載 xml 檔案,而不是我有一個組合框,它顯示 xml 檔案中的某些元素。通過在組合框中選擇一個專案,代碼應該知道將資料放在哪里(來自datagridview,所有資料都放在一個類中。但在附加資料之前,我的代碼似乎無法找到我想要的位置添加資料。
所以主要問題是如何從 XML 檔案中搜索內部文本,其次如何在該特定位置添加我想要的資料?
我擁有的 XML 檔案示例如下:
<Root>
<CompanyProfile>
<CompanyName>ABB</CompanyName>
<SiteName>EWP</SiteName>
<ClientNumber>99999</ClientNumber>
<Imo>987654</Imo>
<MachineTotal>2</MachineTotal>
</CompanyProfile>
<MachineProfile>
<MachineName>Bowthruster</MachineName>
<SerialNumber>123456</SerialNumber>
<TypeNumber>213654</TypeNumber>
<Type>SKA</Type>
<NominalSpeed>1500</NominalSpeed>
<Frequency>50</Frequency>
<NominalPower>15000</NominalPower>
</MachineProfile>
<MachineProfile>
<MachineName>Stuurboord</MachineName>
<SerialNumber>66666</SerialNumber>
<TypeNumber>999987</TypeNumber>
<Type>Synchrone</Type>
<NominalSpeed>3000</NominalSpeed>
<Frequency>50</Frequency>
<NominalPower>17000</NominalPower>
</MachineProfile>
</Root>
我試圖用于查找正確機器并添加資料的代碼在這里:
private void btnDone_Click(object sender, EventArgs e)
{
string SelectedMachine = cmbMachineName.SelectedItem.ToString();
XmlDocument ClientFile = new XmlDocument();
//Create XmlDocument
ClientFile.Load(frmMain.ClientFileName);
//Load the file as selected in the main form
XmlNodeList NodeList = ClientFile.SelectNodes("MachineName");
foreach(XmlNode Node in NodeList)
{
if (Node.ToString() == SelectedMachine)
{
//Here I wanted to add data from the class I have created
}
}
}
分配給該類的資料是這樣編碼的......而且這個作業我已經通過除錯器檢查了它,所以所有的值和描述都是好的......據我所知,當我嘗試搜索時,問題就在上面找到正確的位置并添加資料。請注意,這是我用于分配資料的代碼的一部分,這只是為了顯示我做了什么以及如何做的。可能有人說這不是最好的方法,但那是題外話(盡管我喜歡學習)
XmlMachineMeasurement NewMachineMeasurement = new XmlMachineMeasurement();
//Check for measuringpoint and add to class
for (int i = 0; i < TotalMeasurements; i )
{
MeasuringPoint = dataGridView1.Rows[i].Cells[0].Value.ToString();
switch (MeasuringPoint)
{
case "H1":
NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
NewMachineMeasurement.RMShorDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
break;
case "V1":
NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
NewMachineMeasurement.RMSvertDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
break;
case "A1":
NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
NewMachineMeasurement.RMSaxDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
break;
case "H2":
NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
NewMachineMeasurement.RMShorNDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
break;
case "V2":
NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
NewMachineMeasurement.RMSvertNDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
break;
case "A2":
NewMachineMeasurement.MeasurementDate = dataGridView1.Rows[i].Cells[1].Value.ToString();
NewMachineMeasurement.RMSaxNDE = dataGridView1.Rows[i].Cells[4].Value.ToString();
break;
}
}
uj5u.com熱心網友回復:
真正簡單地使用 Xml Linq :
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication23
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string machineName = "Bowthruster";
XDocument doc = XDocument.Load(FILENAME);
XElement machine = doc.Root.Elements("MachineProfile").Where(x => (string)x.Element("MachineName") == machineName).FirstOrDefault();
machine.Add(new XElement("Tag_NAME", "Value"));
doc.Save(FILENAME);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/495851.html
