我需要序列化为具有多个非嵌套值的数组,即

 <MyArray>
    <Code>code1</Code>
    <Name>name associated with code 1</Name>
    <Code>code2</Code>
    <Name>name associated with code 2</Name>
    <Code>code3</Code>
    <Name>name associated with code 3</Name>
    <Code>code4</Code>
    <Name>name associated with code 4</Name>
  </MyArray>

我在我的阵列上尝试了各种属性 - 例如
[XmlArray(ElementName="MyArray")]
[XmlArrayItem(ElementName="")]
public List<MyPair> MyPairs { get; set; }

注意:MyPair 对象包含 2 个字符串属性(代码和名称):

但无济于事,我总是为每对获得一个包含元素(这通常会更好,但不是架构要求的 - 而且我无法控制)。非常感谢任何帮助。

编辑 这是一个巨大的 xml 文档的一部分,是否可以对其中的一部分使用 XElement 的手动序列化,对其余部分使用 XMLSerialization?

最佳答案

除了手动序列化您的项目之外,我看不到其他方法。

XElement xElem = new XElement("MyArray",
                               array.Select(m => new XElement[] {
                                               new XElement("Code", m.Code),
                                               new XElement("Name", m.Name) })
                              );
var xml = xElem.ToString();

10-06 12:34
查看更多