嵌套对象的 IList 和自定义 XML 序列化

发布于 2024-12-27 19:39:55 字数 1467 浏览 0 评论 0原文

我正在尝试序列化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远昼 2025-01-03 19:39:55

如果您遵循@Henk Holterman的建议,您还想将目标上的WriteXML更改为

writer.WriteAttributeString("Name",Name);

您也可以使用而不是WriteXml(),

XmlSerializer xmlSerializer = new XmlSerializer(target.GetType());
xmlSerializer.Serialize(writer, target);

无论哪种方式都应该给您:

 <?xml version="1.0" encoding="utf-16" ?> 
<SerializeTarget>
  <Name /> 
 <Targets>
 <Target>
  <Target Name="foo" /> 
  </Target>
 <Target>
  <Target Name="foo2" /> 
  </Target>
 <Target>
  <Target Name="foo3" /> 
 </Target>
 </Targets>
</SerializeTarget>

If you follow @Henk Holterman advice you also want to change the WriteXML on target to be

writer.WriteAttributeString("Name",Name);

you can also use instead of WriteXml()

XmlSerializer xmlSerializer = new XmlSerializer(target.GetType());
xmlSerializer.Serialize(writer, target);

Either way should give you:

 <?xml version="1.0" encoding="utf-16" ?> 
<SerializeTarget>
  <Name /> 
 <Targets>
 <Target>
  <Target Name="foo" /> 
  </Target>
 <Target>
  <Target Name="foo2" /> 
  </Target>
 <Target>
  <Target Name="foo3" /> 
 </Target>
 </Targets>
</SerializeTarget>
千秋岁 2025-01-03 19:39:55

这看起来很简单:

 foreach (var target in Target)
 {
     ///??????
     target.WriteXml(writer);
 }

这有问题吗?

编辑:但是您可能还需要 Start 和 End 元素,它们应该位于 Target 方法内部:

public void WriteXml(System.Xml.XmlWriter writer)
{
    writer.WriteStartElement("Target");
    writer.WriteString(Name);
    writer.WriteEndElement(); 
}

That seems very easy:

 foreach (var target in Target)
 {
     ///??????
     target.WriteXml(writer);
 }

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:

public void WriteXml(System.Xml.XmlWriter writer)
{
    writer.WriteStartElement("Target");
    writer.WriteString(Name);
    writer.WriteEndElement(); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文