简体   繁体   中英

C# XMLWriter + prevent “/” “<” “<” chars

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?

您可以使用WriteRaw或将内容包含在<![CDATA[ content goes here ]]>

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();

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.

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 <

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM