0

I got the codes below to encode XML content in String. It's working fine to encode a single tag, but not all the tags within.

String a = @"<main>" + "<Title title=\"Hello & <> World\" />" + "<Content content=\"bla bla <tt> bla... by ? & c1% to ??? on other bla bla....\" />" + "</main>"; String b = a.MakeXMLCompatible(); MessageBox.Show(a + "\n\n" + b); static class SubstringExtensions { public static String MakeXMLCompatible(this String value, String tag = "") { String oldStr = value.Between(tag + "=\"", "\" />"); String newStr = System.Security.SecurityElement.Escape(oldStr); if (oldStr == "") { return ""; } Int32 contentAttribContentValueStart = value.IndexOf(tag + "=\"") + (tag + "=\"").Length; Int32 contentAttibContentValueEnd = value.IndexOf("\" />", contentAttribContentValueStart); return String.Concat(value.Substring(0, contentAttribContentValueStart), newStr, value.Substring(contentAttibContentValueEnd)); } public static String Between(this String value, String a, String b, Boolean useLastIndex = false) { int posA = value.IndexOf(a); if (posA == -1) { return ""; } int posB = (useLastIndex ? value.LastIndexOf(b, posA) : value.IndexOf(b, posA)); if (posB == -1) { return ""; } int adjustedPosA = posA + a.Length; if (adjustedPosA >= posB) { return ""; } return value.Substring(adjustedPosA, posB - adjustedPosA); } } 

How can I enhance the existing codes so that all tags will be encoded?

2
  • have you read stackoverflow.com/questions/157646/… Commented Oct 4, 2016 at 8:47
  • If Qt is not too much weight for your project, you might want to read into the QDomDocument class. You can directly read and write into that, access the elements in a tree structure and convert from and into a QString containing XML code, and thereby also into std::string. Commented Oct 4, 2016 at 9:02

1 Answer 1

0

I resolved the problem by using the codes below:

static class SubstringExtensions { public static String MakeXMLCompatible(this String value, String tag = "") { Int32 valueLength = value.Length; Int32 currentIdx = 0; String tmp = value; while (currentIdx < valueLength) { String oldStr = tmp.Between(tag + "=\"", "\" />", currentIdx); String newStr = System.Security.SecurityElement.Escape(oldStr); if (oldStr == "") { break; } Int32 contentAttribContentValueStart = tmp.IndexOf(tag + "=\"", currentIdx) + (tag + "=\"").Length; Int32 contentAttibContentValueEnd = tmp.IndexOf("\" />", contentAttribContentValueStart); tmp = String.Concat(tmp.Substring(0, contentAttribContentValueStart), newStr, tmp.Substring(contentAttibContentValueEnd)); currentIdx = contentAttibContentValueEnd; } if (currentIdx == 0) { return ""; } else { return tmp; } } public static String Between(this String value, String a, String b, Int32 StartIndex = 0, Boolean useLastIndex = false) { int posA = value.IndexOf(a, StartIndex); if (posA == -1) { return ""; } int posB = (useLastIndex ? value.LastIndexOf(b, posA) : value.IndexOf(b, posA)); if (posB == -1) { return ""; } int adjustedPosA = posA + a.Length; if (adjustedPosA >= posB) { return ""; } return value.Substring(adjustedPosA, posB - adjustedPosA); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.