[XmlIgnore()]
public List<MyObject> ListMyObjects = new List<MyObject>();
public List<string> MyProperty
{
get
{
List<string> list = new List<string>();
foreach (MyObject obj in ListMyObjects)
list.Add(obj.Name);
return list;
}
set
{
foreach (string name in value)
ListMyObjects.Add(new MyObject(name));
}
}
为什么MyPropery没有序列化?我正在使用Xml Serializer。
答案 0 :(得分:1)
有人请格式化我的代码,它不是在iPad上格式化。
请试试这个,
public string[] MyProperty
{
get
{
List<string> list = new List<string>();
foreach (MyObject obj in ListMyObjects)
list.Add(obj.Name);
return list.ToArray();
}
set
{
foreach (string name in value)
ListMyObjects.Add(new MyObject(name));
}
}
答案 1 :(得分:0)
对我来说工作正常..检查一下..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace Serialization_Xml
{
class Program
{
static void Main(string[] args)
{
Data data = new Data();
List<string> mylist = new List<string>();
mylist.Add("A");
mylist.Add("B");
data.MyProperty = mylist;
FileStream fs = new FileStream("Data.xml", FileMode.Create);
XmlSerializer serializer= new XmlSerializer(typeof(Data));
serializer.Serialize(fs, data);
}
}
public class Data
{
[XmlIgnore()]
public List<MyObject> ListMyObjects = new List<MyObject>();
public List<string> MyProperty
{
get
{
List<string> list = new List<string>();
foreach (MyObject obj in ListMyObjects)
list.Add(obj.Name);
return list;
}
set
{
foreach (string name in value)
ListMyObjects.Add(new MyObject(name));
}
}
}
public class MyObject
{
public string Name;
public MyObject(string name)
{
Name = name;
}
}
}
输出:
<?xml version="1.0"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyProperty>
<string>A</string>
<string>B</string>
</MyProperty>
</Data>