Partial interception of default XML serialization C#

I am trying to create an XML object serialization base class to accumulate a bunch of stuff that I’ve learned over the past few years, to have a ready to use example whenever I need it.

I want to add Xml comment functionality, and have found some stackoverflow post that helps me in that. With a bit of cleanup, here is what my code looks like:

// XmlComment attribute class
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class XmlCommentAttribute(string value) : Attribute
{
    public string Value { get; set; } = value;
}

// Usage in code
[XmlComment("This is an example comment that describes the name.")]
[XmlComment("Second comment line")]
public string Name { get; set; }

// Added code to satisfy IXmlSerializable
public void WriteXml(XmlWriter writer)
{
    var properties = GetType().GetProperties();

    foreach (var propertyInfo in properties)
    {
        if (HasCommentAttribute(propertyInfo))
            HandleCommentAttribute(writer, propertyInfo);
 
        writer.WriteElementString(propertyInfo.Name, propertyInfo.GetValue(this).ToString());
    }
}

private static bool HasCommentAttribute(PropertyInfo propertyInfo) 
    => propertyInfo.IsDefined(typeof(XmlCommentAttribute), false);

private static void HandleCommentAttribute(XmlWriter writer, PropertyInfo propertyInfo)
{
    var comments = propertyInfo.GetCustomAttributes(typeof(XmlCommentAttribute), false);

    foreach (var comment in comments)
        if (comment is XmlCommentAttribute line)
            writer.WriteComment($" {line.Value} ");
}

Serialization result:

<!-- This is an example comment that describes the name. -->
<!-- Second comment line -->
<Name>John</Name>

Huray!, although, not quite. When I use the given WriteXml function, I run into issues where object do not serialize properly.

[XmlComment("This is an example comment that describes the name.")]
[XmlComment("Second comment line")]
public string Name { get; set; }

// Added class definition and property below
public class AttributeExample
{
    [XmlAttribute]
    public int Attrib1 { get; set; } = ushort.MaxValue;

    [XmlAttribute]
    public int Attrib2 { get; set; } = byte.MaxValue;

    [XmlAttribute]
    public int Attrib3 { get; set; } = short.MaxValue;
}

public AttributeExample ExampleOfAttributedObject { get; set; }

This serializes to:

<!-- This is an example comment that describes the name. -->
<!-- Second comment line -->
<Name>John</Name>
<ExampleOfAttributedObject>ConfigurationPersistence.ExampleConfigObject2+AttributeExample</ExampleOfAttributedObject>

Which is not quite correct.

The problem lies in the fact that the WriteXml function parses my object to a string with the writer.WriteElementString(propertyInfo.Name, propertyInfo.GetValue(this).ToString()) function call.

How do I get this object, and any of its child objects to serialize properly, like the standard implementation of WriteXml? How does the XmlSerializer do this? You have to iterate over all properties, and that property’s properties, and serialize every object’s value.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật