【问题标题】:How to write XmlType for node names of classes that inherit from a base class using XmlSerializer如何使用 XmlSerializer 为从基类继承的类的节点名称编写 XmlType
【发布时间】:2016-04-18 13:09:37
【问题描述】:

我想从一组 C# 对象生成以下 XML:

<?xml version="1.0" encoding="utf-16"?>
<MyRoot xmlns="http://sample.com">
  <Things>
    <This>
      <ThisRef>this ref</ThisRef>
    </This>
    <That>
      <ThatRef>that ref</ThatRef>
    </That>
  </Things>
</MyRoot>

不幸的是,XML 节点 Things 可以包含多个 This 或 That 节点,我无法控制 XML Schema .我需要创建可以编写正确 XML 的 C# 对象,但我在使用 Things 集合时遇到了问题。

这是我目前所拥有的:

[XmlRoot("MyRoot", Namespace = XmlNamespace)]
public class MyRoot
{
    public const string XmlNamespace = "http://sample.com";
    public List<MyThing> Things { get; set; }

}
[XmlInclude(typeof(This))]
[XmlInclude(typeof(That))]
public class MyThing
{
}
[XmlRoot(Namespace = MyRoot.XmlNamespace)]
[XmlType("This")]
public class This : MyThing
{
    public string ThisRef { get; set; }
}
[XmlRoot(Namespace = MyRoot.XmlNamespace)]
[XmlType("That")]
public class That : MyThing
{
    public string ThatRef { get; set; }
}
[TestClass]
public class so
{
    [TestMethod]
    public void SerializeTest()
    {
        // Create object to serialize
        var data = new MyRoot { Things = new List<MyThing>() };
        data.Things.Add(new This { ThisRef = "this ref" });
        data.Things.Add(new That { ThatRef = "that ref" });

        // Create XML namespace
        var xmlNamespaces = new XmlSerializerNamespaces();
        xmlNamespaces.Add(string.Empty, MyRoot.XmlNamespace);

        // Write XML
        var serializer = new XmlSerializer(typeof(MyRoot));
        using (var writer = new StringWriter())
        {
            serializer.Serialize(writer, data, xmlNamespaces);
            var xml = writer.ToString();
            Console.WriteLine(xml);
        }
    }
}

生成以下 XML:

<?xml version="1.0" encoding="utf-16"?>
<MyRoot xmlns="http://sample.com">
  <Things>
    <MyThing d3p1:type="This" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
      <ThisRef>this ref</ThisRef>
    </MyThing>
    <MyThing d3p1:type="That" xmlns:d3p1="http://www.w3.org/2001/XMLSchema-instance">
      <ThatRef>that ref</ThatRef>
    </MyThing>
  </Things>
</MyRoot>

所以,我有两个问题:

  1. 如何让 XmlSerializer 写入 This 和 That 节点而不是 MyThing 节点?
  2. 如何阻止 XmlSerializer 向 This 和 That 节点(当前写为 MyThing 节点)添加额外的命名空间信息?

【问题讨论】:

  • 不能。相反,序列化程序创建具有值“this”和“that”的“type”属性。您必须编写自己的序列化程序。
  • @jdweng 感谢您的评论,但我找到了解决方案。

标签: c# xml xmlserializer


【解决方案1】:

发布到 stackoverflow 后立即引导您找到答案的原因是什么?我只需要执行以下操作:

[XmlRoot("MyRoot", Namespace = XmlNamespace)]
public class MyRoot
{
    public const string XmlNamespace = "http://sample.com";
    [XmlArrayItem(Type = typeof(This))]
    [XmlArrayItem(Type = typeof(That))]
    public List<MyThing> Things { get; set; }
}
public class MyThing
{
}
public class This : MyThing
{
    public string ThisRef { get; set; }
}
public class That : MyThing
{
    public string ThatRef { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多