Linq to xml - access deep element - c#

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"

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']");

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

Related

How to get an element where the attribute by which I select it can change in Selenium Webdriver C#?

I want to get the src attribute of some elements like this:
<div class="product-tile restored" data-v-27bc931c=""><a href="/p/apple-iphone-11-pro/R1NFM0000000T7"
class="product-tile__link">
<div class="product-tile__image product-img-front">
<img alt="Apple iPhone 11 Pro"
data-src="/img?width=300&height=300&action=resize&url=/catalog/product/6/f/6f7466aef1ae14ee4fe9764531ef69a1.jpg"
src="/img?width=300&height=300&action=resize&url=/catalog/product/6/f/6f7466aef1ae14ee4fe9764531ef69a1.jpg"
lazy="loaded">
<div class="image-loading"></div>
<!---->
</div>
<div class="product-tile__description">
<div title=" iPhone 11 Pro" class="product-tile__name"><span class="product-tile__brand">Apple</span> iPhone
11 Pro
</div>
<!---->
</div>
<div class="product-tile__footer">
<div class="product-tile__condition">
<div>Various conditions<span class="colors"><span class="dot">•</span>4 Colors</span></div>
</div>
<div class="product-tile__price">
<div class="price" data-v-678b2a80="">
<div class="current-price" data-v-678b2a80="">
<div class="from" data-v-678b2a80="">from</div>
<div data-testid="productPrice" class="amount" data-v-678b2a80="">
$647.00
</div>
</div>
<div data-v-678b2a80=""><span class="amount amount--del" data-v-678b2a80="">$1049.00</span> <span
class="amount amount--save" data-v-678b2a80="">Save
$402.00</span></div>
</div>
</div>
</div>
<div class="product-tile__loading"></div>
</a>
<!---->
</div>
I'm using driver.FindElement(By.CssSelector("img[lazy='loaded")) but the problem is that this attribute can also be loading. When I try driver.FindElement(By.TagName("img")) that doesn't work.
I don't want to use Try and Catch. How to can I get the src? Here is the website that i want to scrape
To consider both the cases, when value of lazy attribute is loaded and loading, you can use comma seperated values for the css-selectors and use the following locator strategy:
driver.FindElement(By.CssSelector("img[lazy='loaded'], img[lazy='loading']"))
References
You can find a couple of relevant detailed discussions in:
Any built-in way for branching waits with OR conditions?
WebDriverWait for multiple conditions (OR logical evaluation)
selenium two xpath tests in one

How to directly call to c# controller action from angular component html page

I need to call c# controller action from angular component html page in directly without typescript file.
How I write a controller action Home/Subscribe to directly call from
action-xhr="/Home/Subscribe"
<form method="post" class="subscription-form" action-xhr="/Home/Subscribe"
custom-validation-reporting="show-all-on-submit" target="_top" on="submit-error:submit-error.show">
<div class="subscription-form-fieldset">
<div class="subscription-form-div">
<label class="subscription-form-label">
<span class="subscription-form-span"></span>
<input on="tap:submit-error.hide" class="subscription-form-input" type="email" placeholder="Type your email..."
name="email" id="email" required>
<span visible-when-invalid="valueMissing" validation-for="email"></span>
<span visible-when-invalid="typeMismatch" validation-for="email">Please, enter a valid email address.</span>
</label>
</div>
<button class="subscription-form-submit" type="submit">
Subscribe
<div submitting class="subscription-form-submitting">
<i class="fa fa-spinner fa-spin"></i>
</div>
</button>
</div>
<div submit-error id="submit-error" class="subscription-form-submit-error">
<template type="amp-mustache">
{{subscribeSubmitErrorMessage}}
</template>
</div>
</form>
Why do you want to do it from html and not from ts file? This is not recommended. Most of the time you have to map your data, handle the errors, and you can’t do that from your html.
You should reconsider your approach

Traverse C# collection from CSHTML in JQUERY

My View(cshtml) has around 10 div tags at root level.
Each div has nested multiple div inside it.
The HTML code is below. As there is foreach loop, i have multiple such DIV
foreach(var result in results)
{
<div class="itemRow" id= value data-List=#invoiceDetail>
<div class="itemStatus paid"><span>Paid</span></div>
<div class="itemDetails" style="display: none;">
<div class="itemContainer">
<div class="left"> <table class="stripes"> </div>
</div>
<div class="itemContainer" id="_orderId"> <h4>Order details</h4> </div>
<div class="itemContainer">
<div class="left"> <strong>Download PDF</strong></a> </div>
<div class="right"> You can download paper invoice </div>
</div>
<div class="border hundra"><div></div></div>
</div>
</div>
}
Each div at root has an attribute data-List from server side. This attribute has a c# Collection as value. I need to read the value from data-List attribute of that particular div and traverse through it.
I tried to use the .attr method but what i received the data type of the collection.
Please suggest a way forward.
Thanks in advance.

Form post to another page

I am fairly new to C# and asp.net and am having trouble showing something submitted in a form. The form element is a tag. The drop-down info is pulled from a data base and displays correctly. It's just getting it to post to another page after the form is submitted is where I am having the problem. Any help would be appreciated.
Contact.aspx:
<form action="Default.aspx" method="post" data-transition="pop">
<div data-role="fieldcontain">
<label for="topEmails">Business Entity ID:</label>
<select name="topEmails" id="topEmails" data-native-menu="false"
runat="server">
</select>
</div>
<input type="submit" data-iconpos="right" data-inline="true"
data-icon="plus" name="sendMessage" id="sendMessage" value="Send Info">
</form>
Contact.aspx.cs:
AdventureWorks2012DataContext db = new AdventureWorks2012DataContext();
var emails = (from b in db.EmailAddresses
select new { b.EmailAddressID, b.BusinessEntityID }).Take(20);
topEmails.DataTextField = "BusinessEntityID";
topEmails.DataValueField = "BusinessEntityID";
topEmails.DataSource = emails;
topEmails.DataBind();
Default.aspx.cs:
FormSuccessBID.InnerHtml = "Business Entity ID: " + Request.Form["topEmails"] + "";
Any ideas why this wouldn't be working?
Update:
Contact.aspx:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2 style="text-align: center;">Contact Kyle</h2>
<form action="Default.aspx" method="post" data-transition="pop">
<div data-role="fieldcontain">
<label for="userFName">First Name:</label>
<input type="text" name="firstName" id="uFName">
</div>
<div data-role="fieldcontain">
<label for="userLName">Last Name :</label>
<input type="text" name="lastName" id="uLName">
</div>
<div data-role="fieldcontain">
<label for="productsCategories">Products:</label>
<select name="productCategories" id="productCategories" data-native-menu="false" runat="server"></select>
</div>
<div data-role="fieldcontain">
<label for="topEmails">Business Entity ID:</label>
<select name="topEmails" id="topEmails" data-native-menu="false" runat="server"></select>
</div>
<input type="submit" data-iconpos="right" data-inline="true" data-icon="plus" name="sendMessage" id="sendMessage" value="Send Info">
</form>
</asp:Content>
You're missing "runat='server'" in your form definition.
In ASP.NET you're also allowed one form per page. Not sure from what you're posted if you're trying to put multiple forms on there, but if you are, it's not going to work.
I think all you need to do to accomplish what you want is have an ASP.NET button on the page, with the postbackURL set to the page you want to go to. (Of course, you need to do this in a server-side form)
I think you're mixing ASP.NET methods with other, different technologies. (From the look of it, you probably used classic ASP or PHP perhaps?)
If you can't use .NET for whatever reason, the ID/name of the field is not going to be what you're expecting. You need to inspect the form post and find its value - something along the lines of "clt00_somethingelse_topEmails". (Again, this is going to be a heck of a lot easier if you use the .NET way of doing things, but you may have a requirement not to)

Basic MVC: If-else logic in cshtml page

I am just learning MVC and I've created a form but I would like to set conditional formatting ... like with an if-else construct. I know this is basic, but can I put it on the form? If so, how do I code that? What I would like to do is gray out the background of a input box if the checkbox is NOT checked. Thanks in advance
<div class="left">
Emulate Access
</div>
<div class="right">
<input type="checkbox" name="Emulate" />
</div>
<div class="clear" />
<div class="left">
Emulate Associate:
</div>
<div class="right">
<input type="text" name="NmAssoc" />
</div>
I would use JQuery http://jquery.com/. It works great for this sort of thing.

Categories

Resources