Я сериализую класс в XML-документ, и я хочу понять, как добавить пользовательский атрибут в несколько полей класса. Я знаю, что мы можем использовать
[XMLAttribute (AttributeName = "CustomAttribute"] для добавления настраиваемого атрибута к одному узлу документа XML, но я не могу понять, как добавить настраиваемый атрибут к нескольким узлам.
Мой класс выглядит следующим образом:
[XmlRoot(ElementName = "customer")]
public class Customer
{
[XmlElement(ElementName = "cust_id")]
public string CustomerId {get; set;}
[XmlElement(ElementName = "cust_name")]
public string CustomerName {get; set; }
[XmlElement(ElementName = "cust_phone")]
public string CustomerPhone {get; set; }
[XmlElement(ElementName = "cust_email")]
public string CustomerEmail {get; set; }
}
И я использую следующий код для сериализации класса в XML:
var xmlSerializer = new XmlSerializer(toSerialize.GetType());
using (var textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
После сериализации вышеприведенного класса я получаю следующий XML:
<customer>
<cust_id>1</cust_id>
<cust_name>John</cust_name>
<cust_phone>12345678</cust_phone>
<cust_email>john@gmail.com</cust_email>
</customer>
Теперь, если какое-либо из полей класса клиента равно нулю, я хочу пометить соответствующие узлы XML как IsNull = "TRUE". Например:
<customer>
<cust_id>1</cust_id>
<cust_name>John</cust_name>
<cust_phone IsNull = "TRUE" />
<cust_email IsNull = "TRUE" />
</customer>
Как мы можем сериализовать класс в XML, как описано выше, чтобы настраиваемые атрибуты устанавливались для нескольких узлов документа XML?
Спасибо,