What you are asking for is validation, and there are several different validation routes available in the .net framework
some examples are IDataErrorInfo , INotifyDataErrorInfo and ValidationRule
which one is right for you depends on what you are doing
WPF is designed to automatically include these in views so provides some good examples such as here but they work just as well for manual validation tests
public class MyClass: IDataErrorInfo
{
public string SomeString1 { get; set; }
public string AnotherString2 { get; set; }
public bool IsValid
=> string.IsNullOrWhiteSpace(Error);
public string Error
=> this["All"];
public string this[string field]
{
get
{
string err = "";
if (field == "All" || "SomeString1" == field)
{
if (SomeString1.Length > 15)
err += "SomeString1 > 15";
if (SomeString1.Length < 5)
err += "SomeString1 < 5";
}
if (field == "All" || nameof(AnotherString2) == field )
{
err += StringLenthRule(AnotherString2, nameof(AnotherString2), 30, 20);
}
return err;
}
}
private string StringLenthRule(string str, string prop,int max, int min)
{
string err = "";
if (str.Length > max)
err += $"{prop} > {max}\n";
if (str.Length < min)
err += $"{prop} < {min}\n";
return err;
}
}
then you would do
MyClass node = new MyClass(xmlNode);
if(node.IsValid)
{
//use class
}
else
{
display(node.Error)
}