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.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CMS>
<Device Name="CM_HOST" Type="TR">
<PortA Connected_BY="Directly">
<Device TB="AR" ParentConnectedToPort="A" Name="Akitio" Cable="20G Passive" />
</PortA>
<PortA Connected_BY="Directly">
<Device TB="AR" ParentConnectedToPort="A" Name=" doc1" Cable="20G Passive" />
</PortA>
</Device>
</CMS>
it should look like this :
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CMS>
<Device Name="CM_HOST" Type="TR">
<PortA Connected_BY="Directly">
<Device TB="AR" ParentConnectedToPort="A" Name="Akitio" Cable="20G Passive" >
<PortA>
<Device TB="AR" ParentConnectedToPort="A" Name=" doc1" Cable="20G Passive" />
</PortA>
</Device>
</PortA>
</Device>
</CMS>
here is the code itself i am trying to run this code:
private void exportToXmlFile(List<Device> 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");
}
here is the Device class that contains all the attributes :
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 => name; set => name = value; }
public string TBType1 { get => TBType; set => TBType = value; }
public string ConnectedBy { get => connectedBy; set => connectedBy = value; }
public string ParentConnectedTo { get => parentConnectedTo; set => parentConnectedTo = value; }
public string FtdiPort { get => ftdiPort; set => ftdiPort = value; }
public string Cable { get => cable; set => cable = value; }
public string ParentName { get => parentName; set => parentName = value; }
public string DeviceConnectedTo { get => deviceConnectedTo; set => deviceConnectedTo = value; }
public decimal PlusPin { get => plusPin; set => plusPin = value; }
public decimal MinusPin1 { get => MinusPin; set => MinusPin = value; }
public int Xmlevel { get => xmlevel; set => xmlevel = value; }
}
need to concatenate the following elements not to add as a new line (: thanks for all...
That's the reason here is the final out put that i would expect :
<?xml version="1.0" encoding="UTF-8"?>
<CMS>
<Device TB="CM_HOST" properties="{'Name':'Host', 'Type' : 'TR' }" >
<PortA Connected_BY= "MiniBot">
<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' }" >
<PortB Connected_BY= "MiniBot">
<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' }">
<PortA Connected_BY= "Directly">
<Device TB="None" properties="{'Type': 'None' , 'FTDI_Port':0 ,'Name':'samsung-USB3','Cable': '20G Passive'}" ></Device>
</PortA>
<PortB Connected_BY= "ParentConnected"></PortB>
<PortE><Device TB="None" properties="{'Type': 'None' , 'FTDI_Port':0 ,'Name':'samsung-USB3','Cable': '20G Passive'}" ></Device></PortE>
</Device>
</PortB>
<PortA Connected_BY= "ParentConnected"></PortA>
<PortE Connected_BY= "None"></PortE>
</Device>
</PortA>
<PortB Connected_BY= "None"></PortB>
</Device>
</CMS>