6

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 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?

2
  • This doesn't work? What exactly happens? Commented Sep 26, 2011 at 16:15
  • I have next exception: System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: There was an error reflecting type 'TableDisplayConfiguration'. ---> System.InvalidOperationException: There was an error reflecting field 'TableDisplayFields'. ---> System.InvalidOperationException: The XML element 'table-display-fields' from namespace 'localhost/configuration/application' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element. Commented Sep 27, 2011 at 7:01

2 Answers 2

1

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 ?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it is. I need separate record-number-field and field elements.
0

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.

1 Comment

Thanks, but this is not that I really want to see. This is simple solution but requires refactoring of existing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.