简体   繁体   中英

C# XmlSerializer Add an Attribute to a Field

I want to add a custom Attribute belonging to a field

Goal is to get the following XML:

<?xml version="1.0"?>
<doc>
    <assembly>
        <name>class1</name>
    </assembly>
    <members>
      <member name="P:class1.clsPerson.isAlive">
            <Element>
            isAlive
            </Element>
            <Description>
            Whether the object is alive or dead
            </Description>
            <StandardValue>
            false
            </StandardValue>
        </member>
    </members>
</doc>

What I currently have:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Xml.Serialization;

 namespace class1
 {
     public class clsPerson
     {
         [XmlElement(ElementName="isAlive")]
         [Description("Whether the object is alive or dead")]
         [StandardValue(false)]
         public bool isAlive { get; set; }
     }

     class Program
     {
         static void Main(string[] args)
         {
             clsPerson p = new clsPerson();
             p.isAlive = true;
             System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
             x.Serialize(Console.Out, p);
             Console.WriteLine();
             Console.ReadLine();
         }
     }
 }

My current Annotation classes:

using System;

namespace class1
{
    internal class StandardValueAttribute : Attribute
    {
        public readonly object DefaultValue;

        public StandardValueAttribute(Object defaultValue)
        {
            this.DefaultValue = defaultValue;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace class1
{
    internal class DescriptionAttribute : Attribute
    {
        private string v;

        public DescriptionAttribute(string v)
        {
            this.v = v;
        }
    }
}

How can I add my custom Attributes like Description and StandardValue to the XMLSerializer?

It looks like you want to reinvent the wheel. if you trying to export a code documentation I suggest you using the built in functionality:

https://msdn.microsoft.com/en-us/library/b2s063f7.aspx

The XML documentation files are then generated and you can even use them in intellisense

The XMLSerializer will just store the content of an instance.

Using xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication14
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            string name = "P:class1.clsPerson.isAlive";

            XElement person = doc.Descendants("member").Where(x => (string)x.Attribute("name") == name).FirstOrDefault();
            person.Add(new object[] {
                new XElement("Description", "Whether the object is alive or dead"),
                new XElement("StandardValue", false)
            });
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM