有可能每个元素上做出不同的属性? [英] It is possible to make different attributes on each element?
问题描述
需要帮助,请!
我的代码是这样做得风生水起:
My code is this done fast:
[XmlRoot("monster")]
public class monster
{
public List<flag> flags { get; set; }
}
public class flag
{
[XmlAttribute("summonable")]
public int summonable { get; set; }
[XmlAttribute("attackable")]
public int attackable { get; set; }
}
public void XML()
{ monster monster = new monster
{
flags = new List<flag>
{
new flag() { summonable = 0 },
new flag() { attackable = 0 }
}
};
}
他是走出好,我使用的列表与LT;>:
He is coming out well , and I'm using List<> :
<monster>
<flags>
<flag summonable="0" attackable="0" />
<flag summonable="0" attackable="0" />
</flags>
</monster>
我需要这样的:
I need like this:
<monster>
<flags>
<flag summonable="0"/>
<flag attackable="0"/>
</flags>
</monster>
感谢大家谁帮我......对不起,我英文不好
Thank everyone who helped me ... Sorry for my bad English
推荐答案
首先,因为你的标志集是固定的,我不会推荐一个列表<旗>
你的怪物
类中。相反,我将有一个标志
的对象,像这样:
First of all, since your set of flags is fixed, I would not recommend a List<flag>
inside your monster
class. Instead I would have a single flag
object, like so:
[XmlRoot("monster")]
public class monster
{
public flags flags { get; set; }
}
public class flags
{
public int summonable { get; set; }
public int attackable { get; set; }
// Add more as required.
}
现在,按要求序列化此为XML,可以引入公共标志[]标志
属性,你与你的固定属性的名称和值填充。这反过来又可以有一个 [ XmlAnyAttribute将]
属性,你的属性名称和值填充数组,像这样:
Now, to serialize this to XML as required, you can introduce a public Flag[] Flags
property that you populate with the names and values of your fixed attributes. That in turn can have an [XmlAnyAttribute]
attribute array that you populate with the attribute name and value, like so:
[XmlRoot("monster")]
public class monster
{
public flags flags { get; set; }
}
public class flags
{
const string SummonableName = "summonable"; // In c# 6.0 use nameof(summonable)
const string AttackableName = "attackable"; // See https://msdn.microsoft.com/en-us/library/dn986596.aspx
[XmlIgnore]
public int summonable { get; set; }
[XmlIgnore]
public int attackable { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
[XmlElement("flag")]
public Flag[] Flags
{
get
{
return new[]
{
new Flag { Name = SummonableName, Value = XmlConvert.ToString(summonable) },
new Flag { Name = AttackableName, Value = XmlConvert.ToString(attackable) },
};
}
set
{
if (value == null)
return;
foreach (var attr in value)
{
if (attr.Name == SummonableName)
summonable = XmlConvert.ToInt32(attr.Value);
else if (attr.Name == AttackableName)
attackable = XmlConvert.ToInt32(attr.Value);
}
}
}
}
public class Flag
{
[XmlIgnore]
public string Name { get; set; }
[XmlIgnore]
public string Value { get; set; }
[XmlAnyAttribute]
public XmlAttribute[] XmlAttributes
{
get
{
var attr = new XmlDocument().CreateAttribute(Name.ToString());
attr.Value = Value;
return new [] { attr };
}
set
{
if (value == null || value.Length == 0)
{
Name = null;
Value = null;
}
else if (value.Length == 1)
{
Name = value[0].Name;
Value = value[0].Value;
}
else
{
throw new ArgumentException("Too many attributes");
}
}
}
}
然后该XML将看起来像:
Then the XML will look like:
<monster>
<flags>
<flag summonable="0" />
<flag attackable="0" />
</flags>
</monster>
样品的小提琴。
这篇关于有可能每个元素上做出不同的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!