<radG:GridTemplateColumn UniqueName="MyText" DataField="MyText" HeaderText="My Text(Server.HtmlEncode)" SortExpression="MyText">
<ItemTemplate>
<%#String.Format("{0} ", Server.HtmlEncode(Eval("MyText").ToString()))%>
</ItemTemplate>
</radG:GridTemplateColumn>
Here the variable MyText is
<?xml......<password>anyvaluehere</password>"
Basically I have a LINQ to SQL table where a raw XML value has been stored as varchar(max) and then getting published on a web page. I want to delete the value of in the middle of the tag password or replace the value with *****
From my above code I have
<%#String.Format("{0} ", Server.HtmlEncode(Eval("MyText").ToString()))%>
But my result should ideally be "<?xml......<password></password>"
or "<?xml......<password>***********</password>"
How to use asp.net / C# to get the "MyText" password cleaned?
You could do this with a little helper function
GridView Markup.
<radG:GridTemplateColumn UniqueName="MyText" DataField="MyText" HeaderText="My Text(Server.HtmlEncode)" SortExpression="MyText">
<ItemTemplate>
<%# ObscurePassword(Eval("MyText"))%>
</ItemTemplate>
</radG:GridTemplateColumn>
On code-behind.
//Warning: minimal code, no error catching mechanisms included.
protected string ObscurePassword(object xmlObj)
{
//var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><main><blah>some value</blah><password>anyvaluehere</password></main>";
var xml = xmlObj.ToString();
XDocument doc = XDocument.Parse(xml);
XElement elm = doc.Descendants("password").First();
elm.Value = "*****";
//elm.ReplaceWith(new XElement("password", "*****"));
var encodedDoc = System.Web.HttpUtility.HtmlEncode(doc.ToString());
//var encodedDoc = System.Security.SecurityElement.Escape(doc.ToString());
return encodedDoc;
}
Update : If you are using a namespace, ie, xmlns, add that too while querying like this
XNamespace xmlns = "http://yoyo.net/";
XElement elm = doc.Descendants(xmlns + "password").First();
Or
string xmlns = "http://yoyo.net/";
XElement elm = doc.Descendants(XName.Get("Stuff", xmlns)).First();
Hope this helps.
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.