I was finally able to create a Xml file with some coding (with lots of help here from some people). I can store the file but the next process is reading it back. This gave me an error: "Unexpected Xml declaration". After searching a bit I understand that a Xml file can have only one time <?xml version=1.0 etc>. But my document has several of these statements in the document. And therefore the code
xmlDocument doc = new XmlDocument
Throws me this error. The question is how do I get rid of all these comments in the xml document.
The document is create with 2 functions:
private void btnSave_Click(object sender, EventArgs e)
{
//Check if all fields are filled in
if (txbCompany.Text == "" || txbSiteName.Text == "" || txbIMO.Text == "")
{
MessageBox.Show("Please fill in all empty fields");
}
else if (NumMachTot.Value == 0)
{
MessageBox.Show("A Client profile needs at least one assigned machine!");
}
else
{
var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VibroManager");
Directory.CreateDirectory(appData);
//Create the Company Profile Class
CompanyProfile companyProfile = new CompanyProfile();
companyProfile.CompanyName = txbCompany.Text;
companyProfile.SiteName = txbSiteName.Text;
companyProfile.Imo = txbIMO.Text;
companyProfile.MachineTotal = (int)NumMachTot.Value;
//Serialization of the companyProfile and append to the document
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(CompanyProfile));
using (var writer = new StreamWriter(Path.Combine(appData, $"{txbCompany.Text}_{txbSiteName.Text}.xml"), true))
{
x.Serialize(writer, companyProfile);
}
//Iterate the datasource list, NOT the DataGridView.
foreach (MachineProfile machineProfile in dsMachineProfile)
{
AppendMachineData(machineProfile, fileName: Path.Combine(appData, $"{txbCompany.Text}_{txbSiteName.Text}.xml"));
}
//Close form and open Main form
this.Hide();
frmMain fMain = new frmMain();
fMain.ShowDialog();
}
}
changed Code:
private void AppendMachineData(MachineProfile machineProfile, string fileName)
{
//Serialization of the MachineProle and append to the document
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(MachineProfile));
var settings = new XmlWriterSettings();
using (var writer = new StreamWriter(fileName, true))
{
settings.Indent = true;
settings.OmitXmlDeclaration = true;
x.Serialize(writer, machineProfile);
}
}
I think in these two functions the problem is created but in fact I do not know why and how. Or maybe there is another way to solve this.
This is the code that I use to read the xml file
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData );
openFileDialog.Filter = "All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlNode node = doc.DocumentElement.SelectSingleNode("/CompanyName");
lblCompanyName.Text = node.InnerText;
}
}
}