简体   繁体   中英

How to combine 2 child elements with identical name into single property using XmlSerializer class objects is C#

I'm working on reading XML files data, problem is that I've would like to combine this:

- <FIELD>
   <FIELD-TYPE>9999</FIELD-TYPE> 
   <FIELD-TEXT>Cash 538,64</FIELD-TEXT> 
  </FIELD>
- <FIELD>
   <FIELD-TYPE>119</FIELD-TYPE> 
   <FIELD-AMOUNT>538.64</FIELD-AMOUNT> 
  </FIELD>

In single object property like this:

[XmlElement("FIELD-TEXT")]
    public string FieldText { get; set; }

[XmlElement("FIELD-AMOUNT")]
    public string FieldAmount { get; set; }

Currently I can read each parent element separately, which gives me this: ![在此处输入图片说明

Is it possible to somehow configure XmlSerializer or objects? I've also thought about adding another property that somehow combine these property using LINQ.

Expected out would need to look like this: 在此处输入图片说明

Haven't tried anything with XmlSerializer object configuration as I'm new to this and don't know where to start.

Any help would be appreciated.

Hope this helps to contact two fields

var list1 =  doc.Descendants("FIELD-TEXT").Concat(doc.Descendants("FIELD-AMOUNT")).InDocumentOrder();            

foreach (var item in list1)
{
    Console.WriteLine(item);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM