C# xml serialization remove jagged array element name

前端 未结 3 925
梦谈多话
梦谈多话 2021-01-15 02:10

I have the below classes:

public class MainRequest
{
    private Request _dataField;

    [XmlElementAttribute(\"Parameters\")]
    public Request Parameters         


        
3条回答
  •  天命终不由人
    2021-01-15 03:02

    It appears that what you’re trying to accomplish isn’t supported natively; there’s no way of applying an XmlElement attribute to a jagged array. See XmlSerializer bug when serializing collection of collections without root element?

    However, what you can do is decompose your Field[][] jagged array into a simple array of a new type – let’s name it Batch – which would in turn contain an array of your Field type. The following code generates the XML you’re after:

    public class MainRequest
    {
        [XmlElementAttribute("Parameters")]
        public Request Parameters { get; set; }
    }
    
    public class Request
    {
        [XmlElementAttribute(IsNullable = true)]
        public RequestSize RequestSize { get; set; }
    
        [XmlElement("BatchEntry")]
        public Batch[] Batches { get; set; }
    }
    
    public class RequestSize
    {
        [XmlAttributeAttribute]
        public string Count { get; set; }
    
        [XmlTextAttribute]
        public string Value { get; set; }
    }
    
    public class Batch
    {
        [XmlElementAttribute("ParameterEntry")]
        public Field[] Fields { get; set; }
    }
    
    public class Field
    {
        [XmlAttributeAttribute(AttributeName = "name")]
        public string Name { get; set; }
    
        [XmlTextAttribute]
        public string Value { get; set; }
    }
    
    public static void Main(string[] args)
    {
        var request = new MainRequest
        {
            Parameters = new Request
            {
                RequestSize = new RequestSize
                {
                    Count = "1",
                    Value = "2",
                },
                Batches = new Batch[]
                {
                    new Batch 
                    { 
                        Fields = new Field[] 
                        { 
                            new Field { Name = "AAA", Value = "111"},
                            new Field { Name = "BBB", Value = "222"},
                            new Field { Name = "CCC", Value = "333"},
                        }
                    }
                }
            }
        };
    
        using (var stream = new MemoryStream())
        using (var reader = new StreamReader(stream))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(MainRequest));
            serializer.Serialize(stream, request);
    
            stream.Seek(0, SeekOrigin.Begin);
            var str = reader.ReadToEnd();
        }
    }
    

    Generated XML:

    
    
      
        2
        
          111
          222
          333
        
      
    
    

    The advantage of this approach is that it would still work if you define multiple batches. For example:

        var request = new MainRequest
        {
            Parameters = new Request
            {
                RequestSize = new RequestSize
                {
                    Count = "2",
                    Value = "5",
                },
                Batches = new Batch[]
                {
                    new Batch 
                    { 
                        Fields = new Field[] 
                        { 
                            new Field { Name = "AAA", Value = "111"},
                            new Field { Name = "BBB", Value = "222"},
                            new Field { Name = "CCC", Value = "333"},
                        }
                    },
                    new Batch 
                    { 
                        Fields = new Field[] 
                        { 
                            new Field { Name = "DDD", Value = "444"},
                            new Field { Name = "EEE", Value = "555"},
                        }
                    }
                }
            }
        };
    

    …would generate:

    
    
      
        5
        
          111
          222
          333
        
        
          444
          555
        
      
    
    

提交回复
热议问题