Verify validation of XML data before displaying

I want to check the xml before displaying it. I am using XPath, not xsl for it. E.g.

<title></title> <url></url> <submit></submit> 

I want to check if XML data is not there. Then do not display it. because I put these values โ€‹โ€‹in <a href=<%#container.dataitem,url%>>new link</a>. Therefore, I want that if the url is empty, do not display the new link, otherwise display it and similarly for the title, if the title is not empty, otherwise it does not display it.

The main problem: I can check how in the ascx.cs file if(iterator.current.value="") not to display it, but the problem is in the ascx file i m givin

 <a href="">new link</a> 

I want the new link not to appear if the url is empty ... Any idea how to check this condition?

+4
source share
4 answers

I saw how this was handled using the asp: Literal control.

In the web form, you will have <asp:Literal id='literal' runat='server' text='<%# GetAnchorTag(container.dataitem) %>' />

And in the code behind you will have:

 protected string GetAnchorTag(object dataItem) { if(dataItem != null) { string url = Convert.ToString(DataBinder.Eval(dataItem, "url")); if(!string.IsNullOrEmpty(url)) { string anchor = /* build your anchor tag */ return anchor; } } return string.Empty; } 

this way you will either output a full anchor tag or an empty string. I donโ€™t know how it will match your name and send nodes, but it solves the problem of displaying a binding.

Personally, I do not like this approach, but I saw it quite a bit.

0
source

Use XPath. Assuming the elements are enclosed in an element named link :

 link[title != '' and url !=''] 

will find link elements, title and url child elements do not contain streaming text nodes. To make it more bulletproof

 link[normalize-space(title) != '' and normalize-space(url) !=''] 

will keep the expression from matching link elements whose title or url children contain spaces.

0
source

If you do not have access to the .cs file for this, you can put the code directly into the .ascx file. Remember that you do not need to put all your code in the code behind the file, it can go directly to the .ascx file.

 <% if(iterator.current.value!="") { %> <a href=<%#container.dataitem,url%>>new link</a> <% } %> 
0
source

what about //a[not(./@href) or not(text()='']

0
source

All Articles