C# XMLWriter + prevent "/" "<" "<" chars
- by flurreh
Hello,
I have a xmlWriter and want to write String which containt chars of "/" "<" "" (which are part of the xml syntax and break the xml code). Here is my c# code:
public Boolean Initialize(String path)
{
Boolean result = true;
XmlWriterSettings settings = new XmlWriterSettings();
settings.CheckCharacters = true;
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
xmlWriter = XmlWriter.Create(path, settings);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("TestData");
isInitialized = true;
return result;
}
public void WriteProducts(List<Product> productList)
{
if (isInitialized == true)
{
foreach (Product product in productList)
{
xmlWriter.WriteStartElement("Product");
xmlWriter.WriteElementString("Id", product.ProdId);
xmlWriter.WriteElementString("Name", product.ProdName);
xmlWriter.WriteElementString("GroupId", product.ProdGroup);
xmlWriter.WriteElementString("Price", product.ProdPrice.ToString((Consts.FORMATTED_PRICE)));
xmlWriter.WriteEndElement();
}
}
}
public void Close()
{
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
The application runs without any errors, but if I look in the xml file, the xml is incomplete because the xmlwriter stops writing the product nodes when a product name contains one of the above mentioned characters.
Is there a way to fix this problem?