1. Make an XML (How to) from users configurations
private string GetXml(bool addId, string id)
{
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmltextWriter = new XmlTextWriter(stringWriter);
xmltextWriter.Formatting = Formatting.Indented;
// Start document
xmltextWriter.WriteStartDocument();
xmltextWriter.WriteStartElement("userConfigs");
if (addId) xmltextWriter.WriteAttributeString("id", id.ToString());
xmltextWriter.WriteStartElement("fields");
///
/// Here we're gonna see what Columns were present in the grid
///
xmltextWriter.WriteAttributeString("unchecked", "true");
ToolStripDropDownMenu parent = fieldsToolStripMenuItem.DropDown as ToolStripDropDownMenu;
for (int counter = 0; counter <>if (!((ToolStripMenuItem)parent.Items[counter]).Checked)
{
xmltextWriter.WriteElementString("field", counter.ToString());
}
}
xmltextWriter.WriteFullEndElement();
///
/// Now let's see how it was sorted
///
xmltextWriter.WriteStartElement("sorted");
xmltextWriter.WriteElementString("key", sortedLast);
xmltextWriter.WriteElementString("value", sortedCritere);
xmltextWriter.WriteFullEndElement();
///
/// Now let's see how it was filtered
///
xmltextWriter.WriteStartElement("filters");
xmltextWriter.WriteElementString("idCountry", cmbFilterCountry.SelectedValue.ToString());
xmltextWriter.WriteElementString("idAgency", cmbFilterAgency.SelectedValue.ToString());
xmltextWriter.WriteFullEndElement();
///
/// Month and date
///
xmltextWriter.WriteStartElement("date");
xmltextWriter.WriteElementString("year", dateLast.Year.ToString());
xmltextWriter.WriteElementString("month", dateLast.Month.ToString());
xmltextWriter.WriteFullEndElement();
// End document
xmltextWriter.WriteFullEndElement();
xmltextWriter.Flush();
xmltextWriter.Close();
stringWriter.Flush();
return stringWriter.ToString();
}
Let's analyse the code: We have a StringWriter which is assigned to stream and we are embedding a XmlTextWriter with it.
XmlTextWriter functions:
XmlTextWriter xmltextWriter = new XmlTextWriter(stringWriter);
xmltextWriter.WriteStartDocument(); // starts the xml document
// starts the new element with specified name .WriteStartElement(string elementName);
xmltextWriter.WriteStartElement("userConfigs");
// adds an attribute .WriteAttributeString(string attributeName, string attributeValue)
xmltextWriter.WriteAttributeString("id", id.ToString());
// starts the new element with InnerText .WriteElementString(string elementName, string innerText)
xmltextWriter.WriteElementString("field", counter.ToString());
//writes the end tag
xmltextWriter.WriteFullEndElement();
//doesn't write the end tag
xmltextWriter.WriteEndElement();
No comments:
Post a Comment