title: c# write from list of objects to XML file
body: <p>i am facing a problem with writing to xml file using XDocument and XElement here is the wrong output file and how it should be.
  this is how the final xml file look like but it does not meet the requirements.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;CMS&gt;
    &lt;Device Name="CM_HOST" Type="TR"&gt;
      &lt;PortA Connected_BY="Directly"&gt;
        &lt;Device TB="AR" ParentConnectedToPort="A" Name="Akitio" Cable="20G Passive" /&gt;
      &lt;/PortA&gt;
      &lt;PortA Connected_BY="Directly"&gt;
        &lt;Device TB="AR" ParentConnectedToPort="A" Name=" doc1" Cable="20G Passive" /&gt;
      &lt;/PortA&gt;
    &lt;/Device&gt;
  &lt;/CMS&gt;
  </code></pre>
  
  <p>it should look like this :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes"?&gt;
  &lt;CMS&gt;
    &lt;Device Name="CM_HOST" Type="TR"&gt;
      &lt;PortA Connected_BY="Directly"&gt;
        &lt;Device TB="AR" ParentConnectedToPort="A" Name="Akitio" Cable="20G Passive" &gt;
          &lt;PortA&gt;
              &lt;Device TB="AR" ParentConnectedToPort="A" Name=" doc1" Cable="20G Passive" /&gt;
          &lt;/PortA&gt;
        &lt;/Device&gt;
      &lt;/PortA&gt;
    &lt;/Device&gt;
  &lt;/CMS&gt;
  </code></pre>
  
  <p>here is the code itself i am trying to run this code:</p>
  
  <pre><code>private void exportToXmlFile(List&lt;Device&gt; list)
          {
              XDocument xdoc = new XDocument(
              new XDeclaration("1.0", "utf-8", "yes"));
              XElement elm = new XElement("CMS");
  
              XElement hostDev = new XElement("Device");
              hostDev.Add(new XAttribute("Name", list.ElementAt(0).Name)); 
              hostDev.Add(new XAttribute("Type", list.ElementAt(0).TBType1));
  
              foreach (Device device in deviceToAddList)
              {
                  if (device.Name != "CM_HOST")
                  {
                      if(device.DeviceConnectedTo=="A")
                      {
                         if (device.ConnectedBy == "Directly")
                          {
                              XElement deviceElem = new XElement("Device");
                              XElement portAelem = new XElement("PortA");
                              portAelem.Add(new XAttribute("Connected_BY", device.ConnectedBy));
                              deviceElem.Add(new XAttribute("TB", device.TBType1));
                              deviceElem.Add(new XAttribute("ParentConnectedToPort", device.ParentConnectedTo));
                              deviceElem.Add(new XAttribute("Name", device.Name));
                              deviceElem.Add(new XAttribute("Cable", device.Cable));
                              portAelem.Add(deviceElem);
                              hostDev.Add(portAelem);
  
                          }
  
                      }
                  }
              }
              elm.Add(hostDev);
              xdoc.Add(elm);
              xdoc.Save(Application.StartupPath + "\\Topology.xml");
  
  
  
  
          }
  </code></pre>
  
  <p>here is the Device class that contains all the attributes :</p>
  
  <pre><code>public class Device
      {
          string name;
          string TBType;
          string connectedBy;
          string parentConnectedTo;
          string ftdiPort;
          string cable;
          string parentName;
          string deviceConnectedTo;
          decimal plusPin;
          decimal MinusPin;
          int xmlevel;
  
          public Device()
          {
              this.Name = "CM_HOST";
              this.ParentName = "None";
              this.TBType1 = "TR";
              this.parentConnectedTo = "PCI";
              this.Xmlevel = 0;
              this.deviceConnectedTo = "None";
          }
  
          public string Name { get =&gt; name; set =&gt; name = value; }
          public string TBType1 { get =&gt; TBType; set =&gt; TBType = value; }
          public string ConnectedBy { get =&gt; connectedBy; set =&gt; connectedBy = value; }
          public string ParentConnectedTo { get =&gt; parentConnectedTo; set =&gt; parentConnectedTo = value; }
          public string FtdiPort { get =&gt; ftdiPort; set =&gt; ftdiPort = value; }
          public string Cable { get =&gt; cable; set =&gt; cable = value; }
          public string ParentName { get =&gt; parentName; set =&gt; parentName = value; }
          public string DeviceConnectedTo { get =&gt; deviceConnectedTo; set =&gt; deviceConnectedTo = value; }
          public decimal PlusPin { get =&gt; plusPin; set =&gt; plusPin = value; }
          public decimal MinusPin1 { get =&gt; MinusPin; set =&gt; MinusPin = value; }
          public int Xmlevel { get =&gt; xmlevel; set =&gt; xmlevel = value; }
      }
  </code></pre>
  
  <p>need to concatenate the following elements not to add as a new line (: 
  thanks for all...</p>
  
  <p>That's the reason here is the final out put that i would expect :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;CMS&gt;
    &lt;Device TB="CM_HOST" properties="{'Name':'Host', 'Type' : 'TR' }" &gt;
        &lt;PortA Connected_BY= "MiniBot"&gt;
          &lt;Device TB="TR" ParentConnectedToPort ='PortB' properties="{'Pins': {'MiniBot_minus_pin': 2, 'MiniBot_pluse_pin': 3}, 'Type': 'TR' , 'FTDI_Port':0 ,'Name':'SV_Board','Cable': '20G Passive' }" &gt;   
            &lt;PortB Connected_BY= "MiniBot"&gt;
              &lt;Device TB="AR" ParentConnectedToPort ='PortB' properties="{'Pins': {'MiniBot_minus_pin': 0, 'MiniBot_pluse_pin': 1},'Type': 'AR' , 'FTDI_Port':0 ,'Name':'StarTechDoc','Cable': '20G Passive' }"&gt;
                  &lt;PortA Connected_BY= "Directly"&gt;
                      &lt;Device TB="None" properties="{'Type': 'None' , 'FTDI_Port':0 ,'Name':'samsung-USB3','Cable': '20G Passive'}" &gt;&lt;/Device&gt;
                  &lt;/PortA&gt;
                  &lt;PortB Connected_BY= "ParentConnected"&gt;&lt;/PortB&gt;
                  &lt;PortE&gt;&lt;Device TB="None" properties="{'Type': 'None' , 'FTDI_Port':0 ,'Name':'samsung-USB3','Cable': '20G Passive'}" &gt;&lt;/Device&gt;&lt;/PortE&gt;
              &lt;/Device&gt;
            &lt;/PortB&gt;
              &lt;PortA Connected_BY= "ParentConnected"&gt;&lt;/PortA&gt;
            &lt;PortE Connected_BY= "None"&gt;&lt;/PortE&gt;
          &lt;/Device&gt;
        &lt;/PortA&gt;
        &lt;PortB Connected_BY= "None"&gt;&lt;/PortB&gt;
          &lt;/Device&gt;
  &lt;/CMS&gt;
  </code></pre>