嵌套对象的 IList 和自定义 XML 序列化
我正在尝试序列化 IList。所以我为此使用 IXmlSerialized。类如下
class SerializeTarget : IXmlSerializable
{
public IList<Target> Targets { get; set; }
public string Name;
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
throw new NotImplementedException();
}
/// <summary>
/// </summary>
/// <param name="writer">
/// The writer.
/// </param>
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteStartElement("SerializeTarget");
writer.WriteElementString("Name", Name);
writer.WriteStartElement("Targets");
foreach (var target in Targets)
{
///??????
}
writer.WriteEndElement();
writer.WriteEndElement();
}
#endregion
}
class Target : IXmlSerializable
{
public String Name { get; set; }
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteString(Name);
}
#endregion
}
如何从 SerializeTarget.Serialize 调用嵌套对象的序列化?
I am trying to serialize IList. So I am using IXmlSerializable for this. The classes are as follows
class SerializeTarget : IXmlSerializable
{
public IList<Target> Targets { get; set; }
public string Name;
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
throw new NotImplementedException();
}
/// <summary>
/// </summary>
/// <param name="writer">
/// The writer.
/// </param>
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteStartElement("SerializeTarget");
writer.WriteElementString("Name", Name);
writer.WriteStartElement("Targets");
foreach (var target in Targets)
{
///??????
}
writer.WriteEndElement();
writer.WriteEndElement();
}
#endregion
}
class Target : IXmlSerializable
{
public String Name { get; set; }
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteString(Name);
}
#endregion
}
How can I call the serialize of the nested object from SerializeTarget.Serialize?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您遵循@Henk Holterman的建议,您还想将目标上的WriteXML更改为
您也可以使用而不是WriteXml(),
无论哪种方式都应该给您:
If you follow @Henk Holterman advice you also want to change the WriteXML on target to be
you can also use instead of WriteXml()
Either way should give you:
这看起来很简单:
这有问题吗?
编辑:但是您可能还需要 Start 和 End 元素,它们应该位于 Target 方法内部:
That seems very easy:
Is there a problem with that?
Edit: But you will probably need Start and End elements too, they ought to go inside the Target method: