Reputation: 275
In C# i need to access element that is nested in many other elements. I could write a query by naming all of elements but that would be very time consuming. Is there any easy way to do this or maybe some generator?
for example i have a xml:
<div id="list_offers"> <div class="container"> <div id="header"> <div class="row"> <div class="space-on-sides"> <div class="sixteen columns relative"> <div class="logo"> <a href="/"> <img src="/images/design_resp/design/logo_profesia.png" width="113" height="76" alt="PROFESIA.SK - práca, zamestnanie, ponuky práce, brigáda, voÄľnĂ© pracovnĂ© miesto" title="PROFESIA.SK - práca, zamestnanie, ponuky práce, brigáda, voÄľnĂ© pracovnĂ© miesto" /> </a> </div> <div class="right-panel"> <a class="login-company small red button nice radius right hide-on-phones margin-on-left" title="Vstup pre firmy" href="/login_person.php?action=company_login">Vstup pre firmy</a> <a class="login-company small red button nice radius right show-on-phones margin-on-left" href="/login_person.php?action=company_login">Firmy</a> <a rel="nofollow" id="login_modal" class="small gray button nice radius right" title="Prihláste sa do konta" href="/login_person.php?action=login">PrihlásiĹĄ</a> <div class="search"> <div class="relative hide-on-phones"> <form name="fulltextsearch" id="fulltextsearch" action="/search.php" method="get" onsubmit="if(document.getElementById('search').value=='HÄľadanie') return false; return true;" class="nice" /> <input name="which_form" type="hidden" value="simple" /> <input name="tab_name" type="hidden" /> <input name="search_anywhere" type="text" id="search" placeholder="HÄľadanie" tabindex="1" class="input-text expand" /> <input name="submit_search_simple" type="image" src="/images/design_resp/design/blank.png" alt="HÄľadaĹĄ" class="ico search-nosprite" /></form> </div> <div class="show-on-phones"> <a title="HÄľadajte ponuky práce podÄľa rĂ´znych kritĂ©riĂ"> </a> </div> </div> <div class="row-header-favorite"> <div class="header-favorite"> <a title="Moje vybranĂ© pracovnĂ© ponuky" class="ico-shopping-cart-toppanel" rel="nofollow" href="/user_details.php?action=show_my_offers&ref_top_panel=157">0</a> </div> </div> </div> </div> </div> </div> </div> </div> </div>
and i need access:
input name="tab_name" type="hidden"
Upvotes: 0
Views: 337
Reputation: 35353
You can use Linq to xml
var xDoc = XDocument.Load(pathToXmlFile); //XDocument.Parse(xmlstring);
var input = xDoc.Descendants("input")
.First(i=>(string)i.Attribute("name")=="tab_name");
XPath can be used too
var input = xDoc.XPathSelectElement("//input[@name='tab_name']");
Upvotes: 2
Reputation: 9399
If you need to access that element on server side, you'll need to add a runat
attribute there (like this: runat="server"
), to make it a server side control. I would strongly suggest making it a control of your page, then, so you can access it like any other variable. You would have to give it an ID, so at the very least you could fetch it through that property.
But! your problem seems to be more client side related, so I strongly, really strongly suggest you use jQuery instead. Get jQuery in your site, and it becomes as easy as:
$("input[name='tab_name']");
Or better yet, if you give it an ID:
$("#idYouGaveTotheField");
Upvotes: 0