Serialize Property as Xml Attribute in Element

后端 未结 2 998
一整个雨季
一整个雨季 2020-11-27 12:35

I have the following class:

[Serializable]
public class SomeModel
{
    [XmlElement(\"SomeStringElementName\")]
    public string SomeString { get; set; }

          


        
相关标签:
2条回答
  • 2020-11-27 12:43

    Kind of, use the XmlAttribute instead of XmlElement, but it won't look like what you want. It will look like the following:

    <SomeModel SomeStringElementName="testData"> 
    </SomeModel> 
    

    The only way I can think of to achieve what you want (natively) would be to have properties pointing to objects named SomeStringElementName and SomeInfoElementName where the class contained a single getter named "value". You could take this one step further and use DataContractSerializer so that the wrapper classes can be private. XmlSerializer won't read private properties.

    // TODO: make the class generic so that an int or string can be used.
    [Serializable]  
    public class SerializationClass
    {
        public SerializationClass(string value)
        {
            this.Value = value;
        }
    
        [XmlAttribute("value")]
        public string Value { get; }
    }
    
    
    [Serializable]                     
    public class SomeModel                     
    {                     
        [XmlIgnore]                     
        public string SomeString { get; set; }                     
    
        [XmlIgnore]                      
        public int SomeInfo { get; set; }  
    
        [XmlElement]
        public SerializationClass SomeStringElementName
        {
            get { return new SerializationClass(this.SomeString); }
        }               
    }
    
    0 讨论(0)
  • 2020-11-27 12:44

    You will need wrapper classes:

    public class SomeIntInfo
    {
        [XmlAttribute]
        public int Value { get; set; }
    }
    
    public class SomeStringInfo
    {
        [XmlAttribute]
        public string Value { get; set; }
    }
    
    public class SomeModel
    {
        [XmlElement("SomeStringElementName")]
        public SomeStringInfo SomeString { get; set; }
    
        [XmlElement("SomeInfoElementName")]
        public SomeIntInfo SomeInfo { get; set; }
    }
    

    or a more generic approach if you prefer:

    public class SomeInfo<T>
    {
        [XmlAttribute]
        public T Value { get; set; }
    }
    
    public class SomeModel
    {
        [XmlElement("SomeStringElementName")]
        public SomeInfo<string> SomeString { get; set; }
    
        [XmlElement("SomeInfoElementName")]
        public SomeInfo<int> SomeInfo { get; set; }
    }
    

    And then:

    class Program
    {
        static void Main()
        {
            var model = new SomeModel
            {
                SomeString = new SomeInfo<string> { Value = "testData" },
                SomeInfo = new SomeInfo<int> { Value = 5 }
            };
            var serializer = new XmlSerializer(model.GetType());
            serializer.Serialize(Console.Out, model);
        }
    }
    

    will produce:

    <?xml version="1.0" encoding="ibm850"?>
    <SomeModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <SomeStringElementName Value="testData" />
      <SomeInfoElementName Value="5" />
    </SomeModel>
    
    0 讨论(0)
提交回复
热议问题