For example I have class to serialize
[Serializable]
class Person
{
[XmlAttribute("name")]
string Name {get;set;}
}
I need to make Name attribute required. How to do this in .NET?
For example I have class to serialize
[Serializable]
class Person
{
[XmlAttribute("name")]
string Name {get;set;}
}
I need to make Name attribute required. How to do this in .NET?
First of all, [Serializable]
is not used by the XML Serializer.
Second, there is no way to make it required.
I believe you are confusing XML with an XSD. If you want your property to always have a value, initialize this property in the constructor, and throw an exception if anyone tries to set it to empty or null.
class Person
{
private string _Name = "Not Initialized";
[XmlAttribute("name")]
string Name {
get { return _Name;}
set {
if(value == null || value==string.Empty) throw new ArgumentException(...);
_Name = value;
}
}
}