Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions OpenXmlFormats/Spreadsheet/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7333,7 +7333,7 @@ public static CT_DataValidation Parse(XmlNode node, XmlNamespaceManager namespac
}
return ctObj;
}

internal void Write(StreamWriter sw, string nodeName)
{
sw.Write(string.Format("<{0}", nodeName));
Expand All @@ -7352,9 +7352,9 @@ internal void Write(StreamWriter sw, string nodeName)
XmlHelper.WriteAttribute(sw, "sqref", this.sqref);
sw.Write(">");
if (this.formula1 != null)
sw.Write(string.Format("<formula1><![CDATA[{0}]]></formula1>", this.formula1));
sw.Write(string.Format("<formula1><![CDATA[{0}]]></formula1>", XmlHelper.EncodeCDATAContent(this.formula1)));
if (this.formula2 != null)
sw.Write(string.Format("<formula2><![CDATA[{0}]]></formula2>", this.formula2));
sw.Write(string.Format("<formula2><![CDATA[{0}]]></formula2>", XmlHelper.EncodeCDATAContent(this.formula2)));

sw.Write(string.Format("</{0}>", nodeName));
}
Expand Down
17 changes: 17 additions & 0 deletions openxml4Net/Util/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public static string ExcelDecodeString(string t)
}

private static readonly char[] xmlEncodeCharsToReplace = { '&', '<', '>', '"' };
private static readonly char[] cdataEncodeCharsToReplace = { '&', '<', '>','\'' ,'"' };

public static string EncodeXml(string xml)
{
Expand All @@ -330,6 +331,22 @@ public static string EncodeXml(string xml)
.Replace("\"", "&quot;");//.Replace("'", "&apos;");
}

public static string EncodeCDATAContent(string xml)
{
// quick check whether needed
if(xml.IndexOfAny(cdataEncodeCharsToReplace) == -1)
{
return xml;
}

return xml
.Replace("&", "&#38;")
.Replace("<", "&#60;")
.Replace(">", "&#62;")
.Replace("\"", "&#34;")
.Replace("'", "&#39;");
}

public static void WriteAttribute(StreamWriter sw, string attributeName, bool value)
{
WriteAttribute(sw, attributeName, value, true);
Expand Down
Loading