代码之家  ›  专栏  ›  技术社区  ›  Doug Clutter

如何使用XmlSerializer为从基类继承的类的节点名编写XmlType

  •  0
  • Doug Clutter  · 技术社区  · 10 年前

    我想从一组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节点 东西 可以包含几个 那个 节点,并且我对XML模式没有任何控制权。我需要创建能够编写正确XML的C#对象,但我在 东西 收集

    以下是我目前的情况:

    [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写入 那个 节点,而不是 我的东西 节点?
    2. 如何阻止XmlSerializer向 那个 节点(当前写为 我的东西 节点)?
    1 回复  |  直到 10 年前
        1
  •  0
  •   Doug Clutter    10 年前

    在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; }
    }
    
    推荐文章