开发者

Serialize xml different elements to several properties in c#

I have next xml:

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

I deserealize that with next code:

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

Then I add new xml element to table-display-fields node:

<table-display-fields>
  <record-number-field name="ID" />
  <field 开发者_JS百科name="NAME/>
  <field name="DESCRIPTION" />
</table-display-fields>

Then add next code to deserealize record-number-field:

[XmlArray("table-display-fields")]
[XmlArrayItem("record-number-field")]
public TableDisplayField[] RecordTableDisplayFields;

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

This doesn't work. How do I deserealize new xml, and save the existing property path?


You must remove XmlArrayItem() attribute.

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

Each object in the TableDisplayItems will be either a field or a record-number-field.

Of course, if you only have one single record-number-field on top of your array, the solution can be much nicer. Is it the case ?


It would help if you can give a more elaborate description on your requirement and then the direction of your approach. If a normal Serializing/Deserializing is what you are looking for you can try the below solution:

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 );
        }
    }
}

and use the below code for writing and reading the serialized data:

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 );
            }
        }
    }
}

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜