对字符串中的所有XML标记进行编码

时间:2016-10-04 08:20:41

标签: c#

我得到了下面的代码来编码String中的XML内容。它可以很好地编码单个标签,但不能编码所有标签。

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

如何增强现有代码,以便对所有代码进行编码?

1 个答案:

答案 0 :(得分:0)

我使用以下代码解决了这个问题:

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