簡體   English   中英

C#XmlSerializer向字段添加屬性

[英]C# XmlSerializer Add an Attribute to a Field

我想添加屬於字段的自定義屬性

目標是獲取以下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>

我目前擁有的:

 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();
         }
     }
 }

我目前的注釋類:

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;
        }
    }
}

如何將自定義屬性(如Description和StandardValue)添加到XMLSerializer?

看起來你想重新發明輪子。 如果您嘗試導出代碼文檔,我建議您使用內置功能:

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

然后生成XML文檔文件,您甚至可以在intellisense中使用它們

XMLSerializer將只存儲實例的內容。

使用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)
            });
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM