views:

238

answers:

4

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?

A: 

You could either use WriteRaw or enclose the content in <![CDATA[ content goes here ]]>

Barry
A: 

You could use the Server.HtmlEncode(string) method from the System.Web.HttpServerUtility class (this will require you to add the assembly), which will transform < to &lt; (for instance)

And then use Server.HtmlDecode(string) to transform &lt; back to <

Shimrod
Why the downvotes ? At least you could explain...
Shimrod
A: 

You need to re-organize your code a little, and use WriteString to write the values, as this encodes "special" characters:

use http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.writestring.aspx

When the Xml is read back (with an Xml reader) the encoded characters will be decoded.

chibacity
+1  A: 

If it is for example product.ProdName that can contain those chars you can do this:

xmlWriter.WriteStartElement("Product");

xmlWriter.WriteElementString("Id", product.ProdId);

xmlWriter.WriteStartElement("Name");
xmlWriter.WriteString(product.ProdName); // or xmlWriter.WriteCData(product.ProdName);
xmlWriter.WriteEndElement();

xmlWriter.WriteElementString("GroupId", product.ProdGroup);
xmlWriter.WriteElementString("Price", product.ProdPrice.ToString((Consts.FORMATTED_PRICE)));

xmlWriter.WriteEndElement();
Don
I tried Dons suggestion and it seems to work. At least he writes more data in the xml file than before. However the xml file is still incomplete because it stops writing a product Name which has no special characters.Is it maybe because the file is too big? 1MB and 38789 lines
flurreh
Are you sure none of those characters are in your other strings? (Like `product.ProdGroup`). Have you tried to change them all to the same as `product.ProdName`?
Don