I am fairly new to programming and have doing well on my own but I could use help with this one.
I have an XML file that I want to pull NodeList[0].FirstChild.ChildNodes[4].InnerText from and fill my check the corresponding items in the CheckedListBox. Part of the challenge is that I am storing the values text in the XML and need to read it back in with the item numbers. In the case below the numbers would be 1,2.
Here are the listbox items:
ANY
Windows 7 SP1 (x64)
Windows 7 SP1 (x86)
Windows Vista SP2 (x64)
Windows Vista SP2 (x86)
Windows XP SP3 (x64)
Windows XP SP3 (x86)
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
For reference, here is the code I am using to store the values in the XML file.
private void buttonSaveToXML_Click(object sender, EventArgs e)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
MemoryStream strm = new MemoryStream();
XmlWriter XmlWrt = XmlWriter.Create("Releases_List2.xml", settings);
try
{
XmlWrt.WriteStartDocument();
XmlWrt.WriteStartElement("Releases_List");
XmlWrt.WriteStartElement("Item");
if (checkedWindowsVersion.CheckedItems.Count != 0)
{
string s = "";
for (int x = 0; x <= checkedWindowsVersion.CheckedItems.Count - 1; x++)
{
s = s + checkedWindowsVersion.CheckedItems[x].ToString() + ",";
}
XmlWrt.WriteElementString("WindowsVersion", s.TrimEnd(",".ToCharArray()));
}
XmlWrt.WriteEndElement();
XmlWrt.WriteEndDocument();
XmlWrt.Flush();
XmlWrt.Close();
MessageBox.Show("Release List Updated");
}
catch
{
throw;
}
}
}
Here is the relevant portion of the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Releases_List>
<Item>
<ReleaseName />
<Date />
<DetectedFile />
<DetectedHash />
<WindowsVersion>Windows 7 SP1 (x64),Windows 7 SP1 (x86)</WindowsVersion>
</Item>
</Releases_List>
This is as close as I have come so far:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "eXtendeble Markup Language (*.xml)|*.xml";
openFileDialog1.FileName = "Releases_List.xml";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlDocument Items = new XmlDocument();
Items.Load(fs);
XmlNodeList NodeList = Items.GetElementsByTagName("Releases_List");
for (int i = 0; i < NodeList.Count; i++)
{
checkedWindowsVersion.Items.AddRange(new object[]
{ NodeList[0].FirstChild.ChildNodes[4].InnerText });
}
}
}
I would appreciate any help.