how to check whether some entered InnerText is present in an XML or not and to give an exception?
I have written a code in C# - XML which checks whether a value is there or not in a given XML document and prints the value and the particular tag associated with the value. When we enter the Value of the Inner Text it will look for the value in the document and find it. I dont understand what exception to catch when the entered value is not there in the document.
I tried doing like this but it is not working.
1.
if (inpXMLString != AppChildNode.InnerText)
throw new InvalidDataException("The entered value" + " " + i开发者_运维知识库npXMLString + " " + "doesnot exist");
Here : inpXMLstring
= entered value;
AppChildNode.InnerText
= value of the tags searched.
2.
catch (System.Xml.XmlException e1)
{
Console.WriteLine(e1.Message);
}
this does not give any exception when the entered value is not there in the XML document.
Please help me in this regard.
Looks like the code is catching a different exception than it is throwing. The code appears to throw a InvalidDataException but catches a System.Xml.XmlException. Here are a few articles on exception handling:
http://msdn.microsoft.com/en-us/library/ms173160(VS.80).aspx
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=128
Something Like this:
void DoSomething()
{
try
{
/*
* Do Something Useful.
*/
CheckValue("Hello");
}
catch (InvalidDataException e)
{
Console.WriteLine(e.Message);
}
}
private void CheckValue(string inpXMLString)
{
if (inpXMLString != AppChildNode.InnerText)
throw new InvalidDataException("The entered value" + " " + inpXMLString + " " + "doesnot exist");
}
精彩评论