C#的Xml序列化列表&LT; T&GT;后代与XML属性 [英] C# Xml Serializing List<T> descendant with Xml Attribute
问题描述
早晨家伙,
我从名单下降,具有公共属性的集合。 XML序列化不拿起我的广告载体。列表项序列罚款。我曾尝试XmlAttribute属性无济于事。难道你们有一个解决方案?
公共部分类主窗口:窗口
{
公共主窗口()
{
的InitializeComponent();
}
私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
变种人=新PersonCollection
{
新的Person {名字=苏,年龄= 17},
新的Person {名字=乔,年龄= 21}
};
people.FavoritePerson =苏; 变种X =新的XmlSerializer(people.GetType());
变种B =新的StringBuilder();
变种W = XmlTextWriter.Create(B,新XmlWriterSettings {NewLineChars =\\ r \\ n,缩进=真});
x.Serialize(W,人);
变种S = b.ToString();
}
}[XmlRoot(的ElementName =人物)
公共类PersonCollection:列表&LT;&人GT;
{
//不行! ARGHHH
[XmlAttribute]
公共字符串FavoritePerson {搞定;组; }
}公共类Person
{
[XmlAttribute]
公共字符串名字{获得;组; }
[XmlAttribute]
公众诠释年龄{搞定;组; }
}
我得到下面的XML
&LT;?XML版本=1.0编码=UTF-16&GT?;
&LT;人的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =http://www.w3.org/2001/XMLSchema&GT;
&LT;人姓=苏年龄=17/&GT;
&LT;人姓=乔年龄=21/&GT;
&LT; /人&GT;
我想获得这个
&LT;?XML版本=1.0编码=UTF-16&GT?;
&LT;人民FavoritePerson =苏的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =http://www.w3.org/2001/XMLSchema&GT;
&LT;人姓=苏年龄=17/&GT;
&LT;人姓=乔年龄=21/&GT;
&LT; /人&GT;
这是不是一个问题的答案,但我想我会做一个建议,在code开发,以缓解。
添加一个新的添加
方法的 PersonCollection
类,例如:
公共类PersonCollection:列表&LT;人&gt;中的IXmlSerializable
{
...
公共无效添加(串名字,年龄INT)
{
this.Add(新的Person(名字,年龄));
}
...
}
然后,通过这样做,可以简化您的集合初始化语法:
变种人=新PersonCollection
{
{苏,17},
{乔,21}
};
people.FavoritePerson =苏;
Morning Guys,
I have a collection that descends from List and has a public property. The Xml serializer does not pick up my proeprty. The list items serialize fine. I have tried the XmlAttribute attribute to no avail. Do you guys have a solution?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var people = new PersonCollection
{
new Person { FirstName="Sue", Age=17 },
new Person { FirstName="Joe", Age=21 }
};
people.FavoritePerson = "Sue";
var x = new XmlSerializer(people.GetType());
var b = new StringBuilder();
var w = XmlTextWriter.Create(b, new XmlWriterSettings { NewLineChars = "\r\n", Indent = true });
x.Serialize(w, people);
var s = b.ToString();
}
}
[XmlRoot(ElementName="People")]
public class PersonCollection : List<Person>
{
//DOES NOT WORK! ARGHHH
[XmlAttribute]
public string FavoritePerson { get; set; }
}
public class Person
{
[XmlAttribute]
public string FirstName { get; set; }
[XmlAttribute]
public int Age { get; set; }
}
I'm getting the following xml
<?xml version="1.0" encoding="utf-16"?>
<People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person FirstName="Sue" Age="17" />
<Person FirstName="Joe" Age="21" />
</People>
I would like to get this
<?xml version="1.0" encoding="utf-16"?>
<People FavoritePerson="Sue" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person FirstName="Sue" Age="17" />
<Person FirstName="Joe" Age="21" />
</People>
This isn't an answer to the question, but I thought I'd make a suggestion to ease in code development.
Add a new Add
method to the PersonCollection
class as such:
public class PersonCollection : List<Person>, IXmlSerializable
{
...
public void Add(string firstName, int age)
{
this.Add(new Person(firstName, age));
}
...
}
Then, by doing this, you can simplify your collection initializer syntax to:
var people = new PersonCollection
{
{ "Sue", 17 },
{ "Joe", 21 }
};
people.FavoritePerson = "Sue";
这篇关于C#的Xml序列化列表&LT; T&GT;后代与XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!