XmlAttribute 不适用于 XmlArray [英] XmlAttribute not working with XmlArray
本文介绍了XmlAttribute 不适用于 XmlArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 XmlSerializer 生成以下 XML 结构时遇到问题:
I'm having trouble producing the following XML-structure using the XmlSerializer:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>This is root.</Name>
<OtherValue>Otha.</OtherValue>
<Shapes Name="This attribute is ignored!">
<Circle>
<Name>This</Name>
<Value>Is</Value>
<Whatever>Circle</Whatever>
</Circle>
<Square>
<Name>And</Name>
<Value>this is</Value>
<Something>Square</Something>
</Square>
</Shapes>
</Root>
唯一的问题是
的属性没有被写入.我用于序列化的类如下:
The only problem is that the attributes of <Shapes>
doesn't get written.
The classes I'm using for the serialization are the following:
public class Root
{
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string OtherValue { get; set; }
[XmlArray("Shapes")]
[XmlArrayItem("Circle", typeof(Circle))]
[XmlArrayItem("Square", typeof(Square))]
public ShapeList Shapes { get; set; }
}
public class ShapeList : List<Shape>
{
// Attribute that is not in output
[XmlAttribute]
public string Name { get; set; }
}
public class Shape
{
public string Name { get; set; }
public string Value { get; set; }
}
public class Circle : Shape
{
public string Whatever { get; set; }
}
public class Square : Shape
{
public string Something { get; set; }
}
还有一个运行序列化的主要方法:
And a little main method to run the serialization:
public static void Main(String[] args)
{
var extraTypes = new Type[] {
typeof(Shape),
typeof(Square),
typeof(Circle)
};
var root = new Root();
root.Name = "This is root.";
root.OtherValue = "Otha.";
root.Shapes = new ShapeList()
{
new Circle() { Name = "This", Value="Is", Whatever="Circle" },
new Square() { Name = "And", Value="this is", Something="Square" }
};
root.Shapes.Name = "This is shapes.";
using (var sw = new StreamWriter("data.xml"))
{
var serializer = new XmlSerializer(typeof(Root), extraTypes);
serializer.Serialize(sw, root);
}
}
- 为什么我没有获得 ShapeList 的 Name 属性?
- 如果使用这种方法无法实现,还有其他简单的方法吗?
推荐答案
不处理数组外部的属性;只处理叶节点 - 集合只是:它们的内容.有一种方法可以做到,如果你不介意让模型更复杂一点......
Attributes are not processed for the outer part of arrays; only leaf nodes are processed for that - collections are just: their contents. There is a way to do it, if you don't mind making the model a bit more complex....
public class Root
{
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string OtherValue { get; set; }
[XmlElement("Shapes")]
public ShapeContainer Shapes { get; set; }
}
public class ShapeContainer
{
[XmlAttribute]
public string Name { get; set; }
private readonly List<Shape> items = new List<Shape>();
[XmlElement("Circle", typeof(Circle))]
[XmlElement("Square", typeof(Square))]
public List<Shape> Items { get { return items; } }
}
public class Shape
{
public string Name { get; set; }
public string Value { get; set; }
}
public class Circle : Shape
{
public string Whatever { get; set; }
}
public class Square : Shape
{
public string Something { get; set; }
}
用法更改如下:
root.Shapes = new ShapeContainer();
root.Shapes.Items.Add(new Circle() { Name = "This", Value="Is", Whatever="Circle" });
root.Shapes.Items.Add(new Square() { Name = "And", Value = "this is", Something = "Square" });
root.Shapes.Name = "This is shapes.";
这篇关于XmlAttribute 不适用于 XmlArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文