C # 코드가 있다고 가정 해 보겠습니다.
class Foo
{
[XmlElement("bar")]
public string[] bar;
}
var foo = new Foo
{
bar = new[] { "1", "2", "3" }
};
어떻게에는 직렬화 할 Foo.bar
로 <bar>1,2,3</bar>
?
배열을 단일 요소로 직렬화하려면 추가 속성을 만들어야합니다.
class Program
{
static void Main(string[] args)
{
var foo = new Foo
{
bar = new[] { "1", "2", "3" }
};
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
serializer.Serialize(Console.Out, foo);
}
}
public class Foo
{
[XmlIgnore]
public string[] bar;
[XmlElement("bar")]
[EditorBrowsable(EditorBrowsableState.Never)]
public string BarValue
{
get
{
if(bar == null)
{
return null;
}
return string.Join(",", bar);
}
set
{
if(string.IsNullOrEmpty(value))
{
bar = Array.Empty<string>();
}
else
{
bar = value.Split(",");
}
}
}
}
산출:
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<bar>1,2,3</bar>
</Foo>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다