[英]C# Xml Serialization Inline Elements
有沒有辦法強制 XML 序列化程序將元素添加為內聯而不是值布局。 我基本上只有一個巨大的結構列表,我想為每個啟用的子元素添加一個內聯元素。
<main>
<item>
<value>1</value>
<name>Alphabet</name>
</item>
...
</main>
我基本上想補充:
<item Enabled="true">
如果元素塊被啟用。 有沒有辦法做到這一點?
是的,只需使用XmlAttributeAttribute
標記 Enabled 屬性:
[XmlAttribute("Enabled")]
public bool Enabled { get; set; }
有關控制 xml 序列化的屬性的文檔可以在 MSDN 上找到: http://msdn.microsoft.com/en-us/library/83y7df3e%28v=VS.100%29.aspx
XmlAttributeAttribute
XmlAttributeAttribute 屬性允許您指定應將成員序列化為屬性以及應命名該屬性的名稱。 只有簡單的數據可以用作屬性,因為屬性只能表示單個值。 坍塌
using System;
using System.Xml.Serialization;
namespace XmlEntities {
[XmlRoot("XmlDocRoot")]
public class RootClass {
private int attribute_id;
[XmlAttribute("id")]
public int Id {
get { return attribute_id; }
set { attribute_id = value; }
}
}
}
這將序列化為與此類似的內容...
<XmlDocRoot id="1" />
更多信息請在 SO 上查看此答案: 如何為 C# XML 序列化添加屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.