2
編碼XML字符串我有一個XML字符串,有時可以包含&。這是代碼(C#):如何用&
var templateDef = String.Format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><template><path>{0}</path><title>{1}</title><description>{2}</description></template>", templateUrl, templateTitle, templateDescription);
return File(System.Text.Encoding.UTF8.GetBytes(templateDef), "application/octet-stream", templateTitle + ".hdtl");
我看了一下HttpUtility.HtmlEncode,但我的問題是,我不能編碼字符串,因爲它會在最後一行再次編碼
return File(System.Text.Encoding.UTF8.GetBytes(templateDef), "application/octet-stream", templateTitle + ".hdtl");
我不能改變這最後一行。一個String.Replace(「&」,「&」)可以工作,但不是一個適當的解決方案。
感謝
使用'XElement'而不是通過構建XML字符串替換。 –