I am a beginner in C#, Xml. If I have to enter a value (partnumber) in TextBox (say 704644-B21) , The partnumber should get searched in XML file, and display if it exists or not.
<Processor>
<Item>
<partnumber>678244-B21</partnumber>
<processorname> E5-2637 Kit</processorname>
<processortype>Dual Core</processortype>
<quantity>2</quantity>
</Item>
</Processor>
<Interconnects>
<Interconnect Bay="Bay1" partnumber="516733-B21">Ethernet - 6120XG Blade Switch</Interconnect>
<Interconnect Bay="Bay2" partnumber="641146-B21">Ethernet - Fabric Ext Module</Interconnect>
<Interconnect Bay="Bay3" partnumber="516733-B21">Ethernet - HP ProCurve 6120XG Blade Switch(516733-B21)</Interconnect>
<Interconnect Bay="Bay4" partnumber="658250-B21">Ethernet - XG Blade Switch</Interconnect>
<Interconnect Bay="Bay5" partnumber="658250-B21">Ethernet - XG Blade Switch</Interconnect>
<Interconnect Bay="Bay6" partnumber="438031-B21">Ethernet - BL-c Switch</Interconnect>
<Interconnect Bay="Bay7" partnumber="451438-B21">Ethernet - Cisco Catalyst Blade Switch 3120G</Interconnect>
<Interconnect Bay="Bay8" partnumber="658250-B21">Ethernet - XG Blade Switch(658250-B21</Interconnect>
</Interconnects>
</ModelDetails>
When i input the part number in the text box, I want to know through Messagebox, if the part number entered in the textbox, exist in the xml file or not. If the Part number exists, Message box should display "Partnumber {0] exist" , else it must display," Part number {0} does not exist."
private bool hasNode;
XmlDocument xworkload = new XmlDocument();
private void btnPnoCheck_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".hpa";
dlg.Filter = "Xml document (.hpa)|*.hpa";
var result = dlg.ShowDialog();
if(result==true)
{
xworkload.Load(dlg.FileName);
string xmlcontents = xworkload.InnerXml;
string textToSearch = textBox1.Text;
bool hasnode = (xworkload.DocumentElement.SelectNodes("//Item/partnumber[text()='" + textToSearch + "']").Count > 0) ;
if (!hasnode)
{
MessageBox.Show(String.Format("Part number {0}{1} found ", textBox1.Text, (!hasNode ? " not" : String.Empty)));
}
else
MessageBox.Show(String.Format("Part number {0} found" , textBox1.Text));
}
}
The above code doesn't counts the partnumber inside 'Interconnects' tag
See SO question: Find an element in xml by its inner text
Search on an event (on a button click? On a textChanged? Depending on your needs) with a method like this:
// Load your document
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlFile);
string textToSearch = YourTextBox.Text;
bool hasNode = (xmlDoc.SelectNodes("/Expansion/ID[text()='" + textToSearch + "']").Count > 0);
Or
bool hasNode = (xmlDoc.SelectSingleNode("/Expansion/ID[text()='" + textToSearch + "']" != null);
EDIT: for your complement: if you want to display a message box only when the item if not found:
MessageBox.Show(String.Format("Item {0}{1} found", textBox1.Text, (!hasNode ? " not" : String.Empty)));