XML-序列化 - 如何在不丢失其 XML-属性的情况下传递 属性-数组

XML-Serialization - How to pass an property-array without it loosing its XML-Attributes

我有两个 class。 classes 之一包含另一个 class.

的数组

我不希望第一个 Class 被序列化,只希望另一个 class 的数组被序列化,所以我将数组传递给序列化方法,但它似乎正在丢失它名称,因为它后来被称为 ArrayOfSecondClass。有人可以帮我解决这个问题吗?

这里是一些描述场景的测试代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace TeaTimeTestEmpty
{
    public class FirstClass
    {
        [XmlArray("RealName")] // Is ignored when SecondClassArray is passed
        public SecondClass[] SecondClassArray { get; set; }
    }
    public class SecondClass
    {
        public string Name { get; set; }
    }
    public static class Program
    {
        public static string SerializeToXml(object objectToSerialize, Type objectType = null)
        {
            if (objectToSerialize != null)
            {
                if (objectType == null)
                {
                    objectType = objectToSerialize.GetType();
                }
                XmlSerializer serializer = new XmlSerializer(objectType);

                using (MemoryStream stream = new MemoryStream())
                {
                    serializer.Serialize(stream, objectToSerialize, null);

                    string xmlString = "";
                    foreach (byte currentByte in stream.ToArray())
                    {
                        xmlString += (char)currentByte;
                    }

                    return xmlString;
                }
            }
            return null;
        }
        static void Main(string[] args)
        {
            List<SecondClass> listOfSecondClasses = new List<SecondClass>();
            for (int i = 0; i < 10; ++i)
            {
                listOfSecondClasses.Add(new SecondClass() { Name = "Bob" + i });
            }

            FirstClass firstClass = new FirstClass() { SecondClassArray = listOfSecondClasses.ToArray()};

            // Note that I am passing only the SecondClassArray, not the whole Element
            string xml = SerializeToXml(firstClass.SecondClassArray);
        }
    }
}

现在,当我调试它时,我在变量 xml 中得到以下 XML-代码:

    <?xml version="1.0"?>
<ArrayOfSecondClass 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <SecondClass>
    <Name>Bob0</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob1</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob2</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob3</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob4</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob5</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob6</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob7</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob8</Name>
  </SecondClass>
  <SecondClass>
    <Name>Bob9</Name>
  </SecondClass>
</ArrayOfSecondClass>

我现在的问题是,我在 FirstClass 中给它的名字丢失了,我似乎找不到找回它的方法,即使我给 Second[=28] =] XmlRoot 或其他标签,它总是调用它 ArrayOfSecondClass 而不是想要的名称。

如果你能给我一个解决方案,告诉我如何给它起我想要的名字,我将不胜感激。

XmlArray 属性是 FirstClass 的一部分,而不是其 属性 的一部分,因此很明显,当序列化器从未看到 [=12= 的实例时,它不会被使用].

您将需要与 another constructor of the XmlSerializer class 一起工作。

此构造函数允许您传递根元素的名称:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "RealName";
XmlSerializer serializer = new XmlSerializer(objectType, xRoot);
static void Main(string[] args)
{
    List<SecondClass> listOfSecondClasses = new List<SecondClass>();
    for (int i = 0; i < 10; ++i)
    {
        listOfSecondClasses.Add(new SecondClass() { Name = "Bob" + i });
    }

    FirstClass firstClass = new FirstClass() { SecondClassArray = listOfSecondClasses.ToArray() };

    // Note that I am passing only the SecondClassArray, not the whole Element
    string xml = SerializeToXml(firstClass.SecondClassArray,"RealNames");

    Console.WriteLine(xml);

    Console.ReadLine();
}

public static string SerializeToXml<T>(T objectToSerialize, string RootNodeName)
{
    if (objectToSerialize != null)
    {

        XmlRootAttribute root = new XmlRootAttribute(RootNodeName);
        XmlSerializer serializer = new XmlSerializer(typeof(T),root);

        using (MemoryStream stream = new MemoryStream())
        {
            serializer.Serialize(stream, objectToSerialize, null);

            string xmlString = "";
            foreach (byte currentByte in stream.ToArray())
            {
        xmlString += (char)currentByte;
            }

            return xmlString;
        }
    }
    return null;
}