Hope you can help me a bit. I'm trying to write to an XML file, but am struggling to write the method which, well, writes to the XML file. This is the XML file manually written (using Notepad++ etc.):
<software>
<software_entry
name="Adobe Acrobat X Standard"
path="Applications\Acrobat\Acrobat X Standard\AcroStan.msi"
type="msi"
switches="/qn ALLUSERS=1"
/>
<software_entry
name="Adobe Acrobat X Professional"
path="Applications\Acrobat\Acrobat X Pro\AcroPro.msi"
type="msi"
switches="/qn ALLUSERS=1"
/>
</software>
The aim of this part of the application is to write that using a GUI.
In the application, the user chooses the name of the XML file. It is then saved in the temp folder until further in the process when the user is asked where they would like to save it. Upon entering the desired name of the file and clicking Create, the method called "createAndLoadXML" is run. As its name would suggest, it creates and then loads an XML file (to populate a listview control on the form). Code can be seen below.
private void createAndLoadXML()
{
// Method to create XML file based on name entered by user
string tempPath = Path.GetTempPath();
string configFileName = fileNameTextBox.Text;
string configPath = tempPath + configFileName + ".xml";
// Create XDocument
XDocument document = new XDocument(
new XDeclaration("1.0", "utf8", "yes"),
new XComment("This XML file defines the software selections for use with the Software Installer"),
new XComment("XML file generated by Software Installer"),
new XElement("software",
new XElement("software_entry",
new XAttribute("name", ""),
new XAttribute("path", ""),
new XAttribute("type", ""),
new XAttribute("switches", ""))
)
);
document.Save(configPath);
configCreateLabel.Visible = true;
document = XDocument.Load(configPath);
}
Now, further down this form are 4 text boxes for user input, each relating to the attributes created (name, path, type and switches) The idea is the user will write in these text boxes, click an 'Add' button and then the program will write those 4 fields as attributes to this XML file. So far, I have this code, which is horribly incomplete and doesn't even use LINQ to XML.
private void writeToXML()
{
// Method to write lines to XML file based on user input
// Sets string variables
string fileName = softwareNameTextBox.Text;
string filePath = filePathTextBox.Text;
string fileType = installerType.Text.ToString();
string installSwitches = installSwitchesTextBox.Text;
using (XmlWriter xw = XmlWriter.Load(configPath)) //This line is wrong, I know
{
xw.WriteStartElement("software");
xw.WriteElementString("name", fileName);
xw.WriteElementString("path", filePath);
xw.WriteElementString("type", fileType);
xw.WriteElementString("switches", installSwitches);
xw.WriteEndElement();
}
}
Basically, could anyone please help me with the above method which writes to the XML the data the user has entered into the text box controls? I'm not sure how to load the previously created XML document (from my createAndLoadXML method), and how to write within the root element (software) using LINQ to XML.
Try this out. I think this should get you what you want assuming the XML exists beforehand since you are calling createAndLoadXML
before this method. I wrote this in NotePad++, so I may have a error or two.
private void writeToXML()
{
// Method to write lines to XML file based on user input
// Sets string variables
string fileName = softwareNameTextBox.Text;
string filePath = filePathTextBox.Text;
string fileType = installerType.Text.ToString();
string installSwitches = installSwitchesTextBox.Text;
string FILE_PATH = "bla.xml";
XDocument xDoc = XDocument.Load(FILE_PATH);
xDoc.Root.Add(new XElement("software_entry",
new XAttribute("name", fileName),
new XAttribute("path", filePath),
new XAttribute("type", fileType),
new XAttribute("switches", installSwitches)
));
xDoc.Save(FILE_PATH);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With