I put reading barcode in separate method which reads the barcode and put it in a textbox named barcode. and created a button that will load data coresponding to that barcode , but facing problem
private void Load_Click(object sender, RoutedEventArgs e)
{
var str = #"<Books xmlns=""""> <book Barcode=""780672318863""><Serial>11</Serial>
<name>abc</name> <detail>Fantasy</detail></book>
<book Barcode=""780672318864""><Serial>12</Serial>
<name>abc</name><detail>Fantasy1</detail></book></Books>";
var strBarcode = barcode.Text;
MessageBox.Show(strBarCode);
XDocument docX = XDocument.Parse(str);
var s = docX.Descendants("book").FirstOrDefault(a => a.Attribute("Barcode").Value == strBarcode);
spnl.DataContext = s;
}
now Messagebox says strBarCode has correct value but it is not showing up in program and
s value is coming out to be null
on other hand if i put directly "780672318863" in place of strBarcode it is showing value correctly
can anyone tell me where i am going wrong ?
Not reproducable.
I ran your code with docX.Descendants("book")... and it produces the correct element.
You could try
string strBarcode = barcode.Text.Trim();
but for the rest you will just have to look around in the debugger.
Related
When copy-pasting an IP address from a share-point site, I can see that the actual value that gets pasted in the text-box is: 123.123.123.123U+200B, which then returns false when attempting to IPAddress.TryParse(...).
I'm rather new in WPF world, and with the help of some SO posts I have so far managed to resolve this by having the PasteHandler in my code-behind class:
private static void TextPasteHandler(object sender, DataObjectPastingEventArgs e)
{
if (!e.DataObject.GetDataPresent(typeof(string))) return;
var pastedText = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
DataObject dObj = new DataObject();
dObj.SetData(DataFormats.Text, Regex.Replace(pastedText, #"[^\u0020-\u007E]", string.Empty));
e.DataObject = dObj;
}
However, I was wondering if there's a more standard WPF way to handle cases like this?
I'm trying to create a program that reads the xml data and pulls through the data based on the xpath input. I have a couple of questions i need help with:
1) It is only pulling through the first node where I want it to pull through all nodes. My code is below, can you advise what I need to amend to do this so rather than just pulling through the first answer it pulls through all the data relevant to that path?
2) I have added a third textbox (which is not coded in yet) that you can input a condition so it works with the first xpath value The example is below:
/Clients/Client1/Name (This is the value)
Clients/Client1[Name = 'Smith'] (this is the condition)
I dont know how to code this part to work wih the first text box that is looking at the value?
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(lPrompt.Text) && !string.IsNullOrEmpty(textBox1.Text))
{
try
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(tbFile.Text);
XPathNavigator xnav = xdoc.CreateNavigator();
bool innerXml = checkBox2.Checked;
if (innerXml)
textBox2.Text = xnav.SelectSingleNode(textBox1.Text).InnerXml.ToString();
else
textBox2.Text = xnav.SelectSingleNode(textBox1.Text).Value.ToString();
}
catch (System.Exception ex)
{
textBox2.Text = ex.Message;
textBox2.ForeColor = System.Drawing.Color.Maroon;
Xpath Program Image
I know there is multiple programs I can use like Xpath Visualizer etc but I need this to be very specific to what I'm working on so I need to have a separate box where I can add conditions.
I'm trying to simply select one string line out a long list strings that are held on a server and seperated with a pipe character. This string is grabbed by a php script and the string line is a list of all the media and folders I have on my server.
In my code I'm getting this information and returning it with the following code:
using (var client = new WebClient())
{
result = client.DownloadString("http://server.foo.com/images/getDirectoryList.php");
}
textBox1.Text = string.Join(Environment.NewLine, result.Split('|'));
And it looks like this:
But when I try to simply click on one of them, my cursor simply just goes to where I've clicked. Like this, I tried to select md-harrier.jpg and my cursor just ends up at the end of jpg:
What I'm really wanting is pictured below. I click on Koala.jpg and the whole thing is highlighted and I have the ability to store the name of what it is I've just clicked on. TO achieve that screen shot I had to click next to Koala.jpg and then drag my mouse along.
Is there anyway I can achieve what I want to achieve?
The key thing to note about this is that I will have no idea how many files will be on the server, nor what they will be called. My php script is grabbing this information and displaying it in my winform text box using the code I have wrote above.
as Simon said you need a ListBox, a ListBox fits here because it allows you to select a line, and you can register to the event of SelectedIndexChanged and store the name that was selected.
to initiate the values do
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
listBox1.Items.AddRange(result.Split('|'));
listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
and on the selectedItemChanged:
string currVal;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
currVal = (string)listBox1.SelectedItem;
}
As you said you have no reason to use TextBox,then by using ListBox you can achieve that in this way;
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
string[] names=result.Split('|');
foreach(string name in names)
{
if(name!="|"&&name!=" ")
{
listbox.Items.Add(name);
}
}
Additionally,if you would like to store selected item in a variable subscribe to ListBox's SelectionChangedEvent and store the selection index in a variable in this way;
int selection=;
private void ListBox1_SelectionIndexChanged(object sender,EventArgs e)
{
selection=ListBox1.SelectedIndex;
}
Hi I am using HtmlAgilityPack to scrap some data from web using c# . Here is the code :
private void button1_Click(object sender, EventArgs e)
{
var url = this.textBox1.Text;
var webGet = new HtmlWeb();
var document = webGet.Load(url);
var metaTags = document.DocumentNode.SelectNodes("//meta");
if (metaTags != null)
{
foreach (var tag in metaTags)
{
var name = tag.Attributes["name"].Value;
var content = tag.Attributes["content"].Value;
this.textBox2.Text = name + " : " + content;
}
}
}
its getting a link from textbox1 and showing the output to textbox2 . Its showing the last available data . I can concate the available data but it will show all data at once. Actually I want to show one data when it is available while others are being processed so that user can realize the scrapping progress.Would anyone please help ??
Assuming the first half of your code is correct, this will show the data one-by-one. Most likely, the foreach loop is executing too quickly for you to notice the "one-by-one" effect.
You could apply an artificial delay if you need to display each result long enough for someone to read. Maybe with a Timer or something similar.
An alternative is to use a multi-line textbox (for example a RichTextBox) and place each result on a new-line. Or a ComboBox, which won't take up as much room. These options allow you to keep the data, without having to overwrite it.
You can user Timer control to delay the display of next value in the loop.
You can also use BackgroundWorker to get this done. However this would add complexity to your code. Please look at Beginners Guide to Threading in .NET: Part 5
Or you can use Thread.Sleep(xx) which will delay the execution for the milliseconds you specify.
Thread.Sleep(2000);
this.textBox2.Text += name + " : " + content + Environment.NewLine;
I am developing a very simple application that parses an XML feed, does some formatting and then displays it in a TextBlock. I've added a hyperLink (called "More..) to the bottom of the page (ideally this would be added to the end of the TextBlock after the XML has been parsed) to add more content by changing the URL of the XML feed to the next page.
The issue I'm experiencing is an odd one as the program works perfectly when in the Windows Phone 7 Emulator, but when I deploy it to the device or debug on the device, it works for the first click of the "More..." button, but the ones after the first click just seem to add empty space into the application when deployed or debugged from the device.
I'm using a Samsung Focus (NoDo) and originally thought this may have had to do with the fact that I may not have had the latest developer tools. I've made sure that I am running the latest version of Visual Studio and am still running into the issue.
Here are some snippets of my code to help out.
I've declared the clickCount variable here:
public partial class MainPage : PhoneApplicationPage
//set clickCount to 2 for second page
int clickCount = 2;
Here is the snippet of code I use to parse the XML file:
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
ListBoxItem areaItem = null;
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
string areaName = String.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "example")
{
areaName = reader.ReadElementContentAsString();
areaItem = new ListBoxItem();
areaItem.Content = areaName;
textBlock1.Inlines.Add(areaName);
textBlock1.Inlines.Add(new LineBreak());
}
}
}
}
}
and the code for when the hyperLink button is clicked:
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
int stringNum = clickCount;
//URL is being incremented each time hyperlink is clicked
string baseURL = "http://startofURL" + stringNum + ".xml";
Uri url = new Uri(baseURL, UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
//increment page number
clickCount = clickCount + 1;
}
It feels like there's a little more debugging to do here.
Can you test where exactly this is going wrong?
is it the click that is not working on subsequent attempts?
is it the HTTP load which is failing?
is it the adding of inline text which is failing?
Looking at it, I suspect it's the last thing. Can you check that your TextBlock is expecting Multiline text? Also, given what you've written (where you don't really seem to be making use of the Inline's from the code snippet I've seen), it might be easier to append add the new content to a ListBox or a StackPanel rather than to the inside of the TextBlock - ListBox's especially have some benefit in terms of Virtualizing the display of their content.