C# Xml 序列化列表具有 Xml 属性的后代
早上好,大家好,
我有一个来自 List 的集合,并且具有公共财产。 Xml 序列化程序不获取我的财产。列表项序列化良好。我尝试过 XmlAttribute 属性但无济于事。你们有解决办法吗?
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; }
}
我收到以下 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>
我想得到这个
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我继续通过实现 IXmlSerialized 解决了这个问题。如果存在更简单的解决方案,请发布!
I went ahead and solved the problem by implementing IXmlSerializable. If a simpler solution exists, post it!
这不是问题的答案,但我想我会提出一个简化代码开发的建议。
向
PersonCollection
类添加一个新的Add
方法,如下所示:然后,通过执行此操作,您可以将集合初始值设定项语法简化为:
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 thePersonCollection
class as such:Then, by doing this, you can simplify your collection initializer syntax to:
如果您不介意必须包装所有列表函数,那么您可以将列表嵌入为类的属性,而不是从它派生。
然后,您可以使用 XmlElement 属性强制将 xml 元素写为平面列表(而不是嵌套)。
If you don't mind having to wrap all of the list functions, then you can embed the list as a property of the class rather than deriving from it.
You'd then use the XmlElement attribute to force the xml elements to be written out as a flat list (rather than being nested).