title: C# : How to parse EDIFACT message using Xml Serializer
body: <p>i have this kind EDIFACT message.</p>
  <pre><code>UNB+IATB:1+NGI+OOS+180918:2003+Export_Dump++TR2+X'
  UNH+1+IFLIRR:15:2:1A'
  FDR+OM+135+160918'
  FDD++INT'
  REF'
  STX+ACT'
  IFD+++C+USD++N'
  APD+:::::::ULN:SVO'
  DAT+708:160918:0915+707:160918:1055'
  STX+FD'
  EQP+J+76W::EIFGN+OM'
  EQI+++++++:::FGN'
  EQD++++++A01'
  SSQ+AVIH:5:5::::0:SSR'
  SSQ+BIKE:5:5::::0:SSR'
  SSQ+BSCT:2:2::::0:SSR+J'
  SSQ+BSCT:5:3::::2:SSR+Y'
  SSQ+INFT:15:10::::5:SSR'
  SSQ+PETC:1:1::::0:SSR+J'
  SSQ+PETC:3:3::::0:SSR+Y'
  SSQ+POXY:1:1::::0:SSR'
  SSQ+SPEQ:5:5::::0:SSR'
  SSQ+STCR:0:0::::0:SSR+J'
  SSQ+STCR:1:1::::0:SSR+Y'
  SSQ+SVAN:1:1::::0:SSR+J'
  SSQ+SVAN:3:3::::0:SSR+Y'
  SSQ+TVLG:5:5::::0:SSR'
  SSQ+TVSM:10:10::::0:SSR'
  SSQ+UMNR:5:5::::0:SSR'
  SSQ+WCOB:0:0::::0:SSR'
  LEG+A01+NXC'
  EQI+J:24:S+J:21:A+J:24:O+J:21:E'
  </code></pre>
  <p>This message continues more than about 1 million line.</p>
  <p>I have used C# Xml Serializer and successfully parsed this message into XML file.  But not correct structure.</p>
  <p>Here's my code:</p>
  <pre><code>    switch (keyword)
                          {
                              case &quot;UNB&quot;:
                                  
                                  parts = specificLine.Split(new char[] { '+', ':' }, StringSplitOptions.RemoveEmptyEntries);
                                  serialization = new XmlSerializer(typeof(UNB));
                                  UNB HeaderText = new UNB(parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]);
                                  writer = XmlWriter.Create(TxtWriter, settings);
                                  serialization.Serialize(writer, HeaderText, EmptyNS);
                                  break;
                              case &quot;UNH&quot;:
                                  parts = specificLine.Split(new char[] { '+', ':' }, StringSplitOptions.RemoveEmptyEntries);
                                  serialization = new XmlSerializer(typeof(UNH));
                                  UNH BodyText = new UNH(parts[1],parts[2],parts[3],parts[4],parts[5]);
                                  writer = XmlWriter.Create(TxtWriter, settings);
                                  serialization.Serialize(writer, BodyText, EmptyNS);
                                  break;
                              case &quot;FDR&quot;:
                                  flightDateInformation Gr0 = new flightDateInformation();
                                  parts = specificLine.Split(new char[] { '+'}, StringSplitOptions.RemoveEmptyEntries);                        
                                  serialization = new XmlSerializer(typeof(flightDateInformation));
                                  flightDateDesignator fdrbody = new flightDateDesignator(parts[1], parts[2], parts[3]);
                                  Gr0.flightDateDesignator = fdrbody;
                                  writer = XmlWriter.Create(TxtWriter, settings);
                                  serialization.Serialize(writer, Gr0, EmptyNS);
                                  break;
  } 
  </code></pre>
  <p><strong>and this is my structure class code example:</strong></p>
  <pre><code>    [XmlRoot(ElementName = &quot;UNB&quot;, IsNullable = false), Serializable]
      public class UNB
      {
          [XmlAttribute]
          public string identifier;
          [XmlAttribute]
          public string version;
          [XmlAttribute]
          public string sender;
          [XmlAttribute]
          public string recipient;
          [XmlAttribute]
          public string dateofpreparation;
          [XmlAttribute]
          public string timeofpreparation;
          public UNB(string identifier, string version,string sender, string recipient, string dateofpreparation, string timeofpreparation)
          {
              this.identifier = identifier;
              this.version = version;
              this.sender = sender;
              this.recipient = recipient;
              this.dateofpreparation = dateofpreparation;
              this.timeofpreparation = timeofpreparation;
          }
          public UNB()
          {
  
          }
  }
  </code></pre>
  <p>And my output XML file like this :</p>
  <pre><code>&lt;UNB identifier=&quot;IATB&quot; version=&quot;1&quot; sender=&quot;NGI&quot; recipient=&quot;OOS&quot; dateofpreparation=&quot;180918&quot; timeofpreparation=&quot;2003&quot; /&gt;&lt;UNH identifier=&quot;1&quot; type=&quot;IFLIRR&quot; version=&quot;15&quot; release=&quot;2&quot; agency=&quot;1A&quot; /&gt;&lt;flightDateInformation&gt;
    &lt;flightDateDesignator airlineCode=&quot;OM&quot; flightNumber=&quot;135&quot; departureDate=&quot;160918&quot; /&gt;
  &lt;/flightDateInformation&gt;&lt;flightLevelInfo flightCharacteristics=&quot;INT&quot; /&gt;&lt;referenceInfomation /&gt;&lt;flightFlags statusIndicator=&quot;ACT&quot; /&gt;&lt;inventoryParametersFD controlType=&quot;C&quot; currencyCode=&quot;USD&quot; isUnderActiveRevControl=&quot;N&quot; /&gt;&lt;additionalproductdetails&gt;
    &lt;departureLocation&gt;ULN&lt;/departureLocation&gt;
    &lt;arrivalLocation&gt;SVO&lt;/arrivalLocation&gt;
  &lt;/additionalproductdetails&gt;&lt;scheduledTiming&gt;
    &lt;qualifier&gt;708&lt;/qualifier&gt;
    &lt;date&gt;160918&lt;/date&gt;
    &lt;time&gt;0915&lt;/time&gt;
  &lt;/scheduledTiming&gt;&lt;scheduledTiming&gt;
    &lt;qualifier&gt;707&lt;/qualifier&gt;
    &lt;date&gt;160918&lt;/date&gt;
    &lt;time&gt;1055&lt;/time&gt;
  &lt;/scheduledTiming&gt;&lt;dcsInformation statusIndicator=&quot;FD&quot; /&gt;&lt;aircraftInformation serviceType=&quot;J&quot; aircraftType=&quot;76W&quot;&gt;
    &lt;eqtRegistrationNumber&gt;EIFGN&lt;/eqtRegistrationNumber&gt;
    &lt;aircraftOwner&gt;OM&lt;/aircraftOwner&gt;
  &lt;/aircraftInformation&gt;&lt;acvInformation acvCode=&quot;FGN&quot; /&gt;&lt;saleableConfiguration configurationCode=&quot;A01&quot; /&gt;
  &lt;newSSR quotaCounterName=&quot;AVIH&quot;&gt;
    &lt;maxQuantity&gt;5&lt;/maxQuantity&gt;
    &lt;availability&gt;5&lt;/availability&gt;
    &lt;counter&gt;0&lt;/counter&gt;
    &lt;quotaType&gt;SSR&lt;/quotaType&gt;
  &lt;/newSSR&gt;&lt;newSSR quotaCounterName=&quot;BIKE&quot;&gt;
    &lt;maxQuantity&gt;5&lt;/maxQuantity&gt;
    &lt;availability&gt;5&lt;/availability&gt;
    &lt;counter&gt;0&lt;/counter&gt;
    &lt;quotaType&gt;SSR&lt;/quotaType&gt;
  &lt;/newSSR&gt;
  &lt;newSSR quotaCounterName=&quot;BSCT&quot; cabinCode=&quot;J&quot;&gt;
    &lt;maxQuantity&gt;2&lt;/maxQuantity&gt;
    &lt;availability&gt;2&lt;/availability&gt;
    &lt;counter&gt;0&lt;/counter&gt;
    &lt;quotaType&gt;SSR&lt;/quotaType&gt;
  &lt;/newSSR&gt;
  </code></pre>
  <p>Now my problem is : Yes my code has worked and parsed successfully into XML file. But not as i want. Each node with only 1 line.</p>
  <p><strong>It's my wanted structure.</strong></p>
  <p><a href="https://i.stack.imgur.com/lpvAw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lpvAw.jpg" alt="Branching Diagram" /></a></p>
  <p>Each node has included to other parent node. Some nodes expand into other nodes. my output XML don't have any parent.</p>
  <p>Can i solve this by improving my code or should try different way?</p>
  <p>If you have any need more details, please kindly ask me? i will give you more details</p>
  <p><strong>UPDATE:</strong> I'm resolved this problem.</p>