I have an ASP.NET treeview on my page. In my codebehind I have the following method:
public string GetSelectedTreeValues(TreeView tv)
{
string taxVal = string.Empty;
StringBuilder textBuilder = new StringBuilder();
string lists = string.Empty;
string cleanedlists = string.Empty;
try
{
TreeNodeCollection checkedNodes = tv.CheckedNodes;
foreach (TreeNode tn in checkedNodes)
{
lists = textBuilder.Append(tn.ValuePath + ",").ToString();
}
// removing any trailing commas
cleanedlists = lists.Substring(0, lists.Trim().Length - 1);
}
catch (Exception ex)
{
new ApplicationException("Error: Getting Tree Nodes", ex);
}
return cleanedlists;
}
The problem is when I do a postback, if I change the values I've selected, they always append to the list of values. I thought the list of values should clear, then reset to the new values.
I've stepped through the code and found the CheckedNodes property is always appending the nodes I check to the list without removing unchecked nodes.
Could you post the part of code that does the treeview binding ?
At which point during page lifecycle are binding and GetSelectedTreeValues called ?
If you can, you should replace the try block content with
cleanedlists = string.Join(",", tv.CheckedNodes.Cast<TreeNode>().Select(tn => tn.ValuePath).ToArray());
Related
I was working on finding out the Common string part in the String list. If we take a sample data set
private readonly List<string> Xpath = new List<string>()
{
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(1)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(2)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(3)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(4)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(5)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(6)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(7)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(8)>H2:nth-of-type(1)",
"BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>SECTION:nth-of-type(9)>H2:nth-of-type(1)"
};
From this, I want to find out to which children these are similar. data is an Xpath list.
Programmatically I should be able to tell
Expected output:
BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV
In order to get this What I did was like this. I separate each item by > and then create a list of items for each dataset originally.
Then using this find out what are the unique items
private IEnumerable<T> GetCommonItems<T>(IEnumerable<T>[] lists)
{
HashSet<T> hs = new HashSet<T>(lists.First());
for (int i = 1; i < lists.Length; i++)
{
hs.IntersectWith(lists[i]);
}
return hs;
}
Able to find out the unique values and create a dataset again. But what happened is if this contains Ex:- Div in two places and it also in every originally dataset even then this method will pick up only one Div.
From then I would get something like this:
BODY>MAIN:nth-of-type(1)>DIV>SECTION
But I need this
BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-
type(3)>DIV>ARTICLE>DIV>DIV>DIV
Disclaimer: This is not the most performant solution but it works :)
Let's start with splitting the first path by > character
Do the same with all the paths
char separator = '>';
IEnumerable<string> firstPathChunks = Xpath[0].Split(separator);
var chunks = Xpath.Select(path => path.Split(separator).ToList()).ToArray();
Iterate through the firstPathChunks
Iterate through the chunks
if there is a match then remove the first element
if all first element is removed then append the matching prefix to sb
void Process(StringBuilder sb)
{
foreach (var pathChunk in firstPathChunks)
{
foreach (var chunk in chunks)
{
if (chunk[0] != pathChunk)
{
return;
}
chunk.RemoveAt(0);
}
sb.Append(pathChunk);
sb.Append(separator);
}
}
Sample usage
var sb = new StringBuilder();
Process(sb);
Console.WriteLine(sb.ToString());
Output
BODY>MAIN:nth-of-type(1)>DIV>SECTION>DIV>SECTION>DIV>DIV:nth-of-type(1)>DIV>DIV:nth-of-type(3)>DIV>ARTICLE>DIV>DIV>DIV>
Parsing the string by the seperator > is a good idea. Instead of then creating a list of unique items you should create a list of all items contained in the string which would result in
{
"BODY",
"MAIN:nth-of-type(1)",
"DIV",
"SECTTION",
"DIV",
...
}
for the first entry of your XPath list.
This way you create a List<List<string>> containing every element of each entry of your XPath list. You then can compare all first elements of the inner lists. If they are equal save that elements value to you output and proceed with all second elements and so on until you find an element that is not equal in all outer lists.
Edit:
After seperating your list by the > seperator this could look something like this:
List<List<string>> XPathElementsLists;
List<string> resultElements = new List<string>();
string result;
XPathElementsLists = ParseElementsFormXPath(XPath);
for (int i = 0; i < XPathElementsLists[0].Count; i++)
{
bool isEqual = true;
string compareElemment = XPathElementsLists[0][i];
foreach (List<string> element in XPathElementsLists)
{
if (!String.Equals(compareElemment, element))
{
isEqual = false;
break;
}
}
if (!isEqual)
{
break;
}
resultElements.Add(compareElemment);
}
result = String.Join(">", resultElements.ToArray());
My logic goes as follows: I want to find the first element that misses a given attribute, add the attribute and then find the next element which misses the element, add it and so fourth.
I find the first element missing the amount attribute in the following way:
private XmlNode GetFirstElementWithoutAmount()
{
string productXPathQuery = "//XML/Products";
XmlNodeList productList = ParentXmlDocument.SelectNodes(productXPathQuery);
foreach (XmlNode element in productList)
{
string passengerXPathQuery = "//XML/Products[ID=" + element.FirstChild.InnerText + "]/Amount";
var amount = element.SelectSingleNode(passengerXPathQuery);
if (amount == null)
{
return element;
}
}
return null;
}
When I've found the first element missing the attribute, the amount is added in the following way:
private XmlNode GetOrCreateChildXMLNode(string NewNodeName, XmlNode ParentXMLNode)
{
if (ParentXMLNode == null)
{
return null;
}
XmlNode NewXMLNode = ParentXMLNode.SelectSingleNode("//" + NewNodeName);
if (NewXMLNode == null)
{
NewXMLNode = ParentXmlDocument.CreateNode(XmlNodeType.Element, NewNodeName, string.Empty);
ParentXMLNode.AppendChild(NewXMLNode);
}
return NewXMLNode;
}
The problems is, that it only adds to the first element, and then the first function always returns the second element, even though there's more elements to come? Any ideas why this is?
You are already inside //XML/Products during your foreach loop. Point directly to subnode.
string passengerXPathQuery = "./Amount";
I am using HtmlAgilityPack to find all items, colours and links to products on a website. I want to be able to find an item on the website by typing in the name and colour inside my application.
So far what I have working is:
The application finds items using only the item name and returns the last thing on the website with that name. There are multiple products with the same name but each have a different colour.
The problem comes in when including colour because it's in a different XPath so it's stored in a different collection.
Here is my code:
HtmlNodeCollection collection = doc.DocumentNode.SelectNodes("//*[contains(#class,'inner-article')]//h1//a");
HtmlNodeCollection collection2 = doc.DocumentNode.SelectNodes("//*[contains(#class,'inner-article')]//p//a");
foreach (var node2 in collection2)
{
string coloursv = node2.InnerHtml.ToString();
strColour = coloursv;
//txtLog.Text += Environment.NewLine + (DateTime.Now.ToString("hh:mm:ss")) + str; - This code returns all colours (If code is ran outside of collection then only last colour in string is returned.
}
foreach (var node in collection)
{
string href = node.Attributes["href"].Value;
var itemname = node.InnerHtml.ToString();
if (itemname.Contains(txtKeyword.Text))
{
txtLog.Text = (DateTime.Now.ToString("hh:mm:ss")) + " - Item Found: " + href + " " + itemname + " " + strColour; //Successfully returns item name, colour and link but always gives last availible on website
}
}
This is because you are continually setting the Text property of a textbox within a loop (so each item will continually overwrite the previous):
foreach (var node in collection)
{
// Omitted for brevity
// This will continually overwrite the contents of your Text property
txtLog.Text = ...;
}
If you want to store multiple items, you'll either need to store the results in some type of a collection object (such as a ListBox, etc.) or by simply concatenating your values into the textbox:
foreach (var node in collection)
{
// Omitted for brevity
var stringToAdd = ...;
txtLog.Text += stringToAdd + Environment.NewLine;
}
You can also accomplish this by using the StringBuilder class to be a bit more efficient:
StringBuilder sb = new StringBuilder();
foreach (var node in collection)
{
// Omitted for brevity
var stringToAdd = ...;
// Append this item to the results
sb.AppendLine(stringToAdd);
}
// Store the results
txtLog.Text = sb.ToString();
I am using code like this:
public void BindControlsToCustomXmlPart()
{
wordApp = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
foreach (Word.ContentControl contentControl in wordApp.ActiveDocument.ContentControls)
{
if (contentControl.Tag == "FieldName")
{
string xPathFieldName = "ns:records/ns:record/ns:FieldName";
contentControl.XMLMapping.SetMapping(xPathFieldName,
prefix, currentWordDocumentXMLPart);
}
What ends up happening is every new field I want to add, I have to repeat this redundant code:
if (contentControl.Tag == "FieldName2")
{
string xPathFieldName2 = "ns:records/ns:record/ns:FieldName2";
contentControl.XMLMapping.SetMapping(xPathFieldName2,
prefix, currentWordDocumentXMLPart);
}
Is there a way that I can write this code once and have the "FieldName" portion get updated for each field dynamically? i.e. have some type of loop that would increment through each xmlnode in an xml file (in this case it would map the xml node FieldName to the content control with a tag of FieldName, and then map the xml node FieldName2 to the content control with a tag of FieldName2
A good start would be creating a function to transform your control and reuse that function multiple times as followed
public contentControl BindControlsOperation(contentControl control, string pFieldName)
{
if (control.Tag == pFieldName)
{
string xPathFieldName = String.Format("ns:records/ns:record/ns:{0}",pFieldName);
control.XMLMapping.SetMapping(xPathFieldName,prefix, currentWordDocumentXMLPart);
}
return control;
}
You could then use it in the following fashion
foreach (Word.ContentControl contentControl in wordApp.ActiveDocument.ContentControls)
{
contentControl = BindControlsOperation(contentControl,"FieldName")
}
Next step would be to have a list of names you want to use for fields and feed it to your algorythm using a for loop
....
List<string> names = "x,y,z";
for(int i=0;i < names.length();i++)
{
wordApp.ActiveDocument.ContentControls[i] = BindControlsOperation(wordApp.ActiveDocument.ContentControls[i],name[i])
}
Hope this helps
In my C# window application I need wild card search on list box.
i.e If I write some text in textbox it should be auto selected in that list box.
List box is binding using datatable e.g lstVendor.datasource = l_dtTable
Findstring() function is finding match only for starting string. But I need if match find at any position in particular text then it should be highlighted.
I am Using below code but not getting index/or even lstVendor.selecteditem = "string" not working.
Indexof() always return -1
string final = "";
foreach (Object lstItem in lstVendor.Items)
{
string s = ((DataRowView)(lstItem)).Row.ItemArray[0].ToString();
if (s.ToLower().Contains(txtVendor.Text.ToLower()))
{
int i = lstVendor.Items.IndexOf(s);
final += s + ",";
}
}
string[] l_strArrVendorList = final.TrimEnd(',').Split(',');
for (int Counter = 0; Counter < l_strArrVendorList.Length; Counter++)
{
lstVendor.SelectedItem = l_strArrVendorList[Counter];
}
Searching may return multiple matched items, this code will find the first matched items:
var firstMatched = listBox1.Items.Cast<DataRowView>()
.Where(v=>Convert.ToString(v.Row[0]).ToLower()
.Contains(txtVendor.Text.ToLower()))
.FirstOrDefault();
if(firstMatched != null) listBox1.SelectedItem = firstMatched;
You can remove the FirstOrDefault() to get the list of matched items and implement some navigation through the matched items.
For VS 2000 support, you have to use this extension class:
public static class EnumerableExtension {
public static IEnumerable<T> Cast<T>(this System.Collections.IEnumerable source){
foreach(var item in source)
yield return (T)item;
}
}
My code above should work OK for you, but looks like you just want your code to be fixed instead, here is the fixed code for you:
//Note that you need to set SelectionMode for your listBox like this:
lstVendor.SelectionMode = SelectionMode.MultiSimple;
foreach (var lstItem in lstVendor.Items.Cast<DataRowView>()) {
string s = lstItem.Row.ItemArray[0].ToString();
if (s.ToLower().Contains(txtVendor.Text.ToLower())) {
lstVendor.SelectedItems.Add(lstItem);
}
}