在C#中将XML不同元素序列化为多个属性

6

I have next xml:

<table-display-fields>
  <field name="NAME/>
  <field name="DESCRIPTION" />
</table-display-fields>

我将使用以下代码进行反序列化:

我将使用以下代码进行反序列化:

[XmlArray("table-display-fields")]
[XmlArrayItem("field")]
public TableDisplayField[] TableDisplayFields;

然后我将新的xml元素添加到table-display-fields节点:

<table-display-fields>
  <record-number-field name="ID" />
  <field name="NAME/>
  <field name="DESCRIPTION" />
</table-display-fields>

然后将下面的代码添加到反序列化记录号字段的代码中:
[XmlArray("table-display-fields")]
[XmlArrayItem("record-number-field")]
public TableDisplayField[] RecordTableDisplayFields;

[XmlArray("table-display-fields")]
[XmlArrayItem("field")]
public TableDisplayField[] TableDisplayFields;

这个不起作用。如何反序列化新的xml,并保存现有的属性路径?


这个不起作用吗?具体是什么情况? - m.edmondson
我有下一个异常: System.Web.HttpUnhandledException (0x80004005):引发了类型为'System.Web.HttpUnhandledException'的异常。---> System.InvalidOperationException:反射类型'TableDisplayConfiguration'时出错。---> System.InvalidOperationException:反射字段'TableDisplayFields'时出错。---> System.InvalidOperationException:XML元素“table-display-fields”来自命名空间'http://localhost/configuration/application'已经存在于当前范围内。使用XML属性为元素指定另一个XML名称或命名空间。 - tbicr
2个回答

1

你必须删除 XmlArrayItem() 属性。

[XmlArray("table-display-fields")] 
public object[] TableDisplayItems;

TableDisplayItems 中的每个对象都将是一个 field 或者一个 record-number-field

当然,如果您的数组顶部只有一个单独的 record-number-field,那么解决方案可能会更好。这是这种情况吗?


是的,我需要单独的记录号字段和字段元素。 - tbicr

0

如果您能提供更详细的要求描述和解决方案的方向,那将会很有帮助。如果您需要普通的序列化/反序列化操作,可以尝试以下解决方案:

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

namespace Serialization01 {
    [XmlRoot( "table-display-fields" )]
    public class TableDisplayFields {
        [XmlElement( "record-number-field" )]
        public string RecordNumberField { get; set; }

        [XmlElement( "field" )]
        public List<string> FieldName { get; set; }

        public TableDisplayFields ( ) {
            FieldName = new List<string>( 5 );
        }
    }
}

使用以下代码来编写和读取序列化数据:

using System.IO;
using System.Xml.Serialization;
using System;

namespace Serialization01 {
    class Program {
        static void Main ( string [] args ) {
            // Initiate the class
            TableDisplayFields t = new TableDisplayFields( );

            t.RecordNumberField = "ID";
            t.FieldName.Add( "NAME" );
            t.FieldName.Add( "DESCRIPTION" );

            TextWriter tw = new StreamWriter( Path.Combine( Environment.CurrentDirectory, "Data.xml" ) );

            XmlSerializer xs = new XmlSerializer( t.GetType( ) );

            xs.Serialize( tw, t );

            tw.Flush( );
            tw.Close( );

            TextReader tr = new StreamReader( Path.Combine( Environment.CurrentDirectory, "Data.xml" ) );
            TableDisplayFields t2 = xs.Deserialize( tr ) as TableDisplayFields;

            Console.WriteLine( "RecordNumberField for t2 is {0}", t2.RecordNumberField );
            foreach ( string field in t2.FieldName ) {
                Console.WriteLine( "Found field '{0}'", field );
            }
        }
    }
}

希望这有所帮助。

谢谢,但这不是我真正想看到的。这是一个简单的解决方案,但需要重构现有的代码。 - tbicr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接