Skip to content

Help with XML

velkropie

Asked Jul 19, 2007, 10:04 AM EDT1 answer
Share
LinkedInX

i need help with this bit of code, any help will be greatly appreaciated...

private void btnWriteXml_Click(object sender, EventArgs e)
{
    XmlTextWriter xtw = null;
    string fileName = "ComboItemName.xml";
    try

{

xtw = new XmlTextWriter(fileName, Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument();
xtw.WriteStartElement("ITEMS");

So this method only adds the current text in the text boxes and re-writes anything else...

xtw.WriteElementString("NAME", this.txtName.Text.ToString());
xtw.WriteElementString("URL", this.txtUrl.Text.ToString());

this method works but i need help with a way to check to see if the items in the list box exits in the XML file, otherwise it duplicates averything...

for (int i = 0; i < this.comboBoxUrl.Items.Count; i++)
{
xtw.WriteElementString("NAME", this.comboBoxUrl.Items[i].ToString());
xtw.WriteElementString("URL", this.comboChannels.Items[i].ToString());
}
for (int j = 0; j < this.comboBoxUrl.Items.Count; j++)
{
 xtw.WriteElementString("URL", this.comboBoxUrl.Items[j].ToString());
}

xtw.WriteEndElement();

xtw.WriteEndDocument();

}

finally

{

if (xtw != null)

xtw.Close();

}

}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    It seems like the reason your tags are duplicated is because you are going through the same combo box twice with the same xml tag "URL". 

    Assuming this might not be the problem, If you want to prevent duplication (in the case where you don't have an existing xml file),  create a HashTable or ArrayList that keeps track of the items you are adding to the XML file and check the collection before adding a tag.
    0