简体   繁体   中英

c# write from list of objects to XML file

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>

Without looking over your code where you writting your Elements an Attributes manually in your XML-File. Why don't you just use XML Serialization? Just create classes with the right structure and but the childs of your element in lists in the class. It is easy to build the structure you want in this way and after that serialize your XML like this:

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(*NameOfYourClass*));
            using (StringWriter writer = new StringWriter())
            {
                using (XmlTextWriter xmlWriter = new XmlTextWriter(writer))
                {
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.Indentation = 4;
                    xmlSerializer.Serialize(xmlWriter, *InstanceOfYourClass*);
                    return writer.ToString());
                }
            }

And why your XML has to 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>

And not 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" >
            <Device TB="AR" ParentConnectedToPort="A" Name=" doc1" Cable="20G Passive" />
        </PortA>
    </Device>
</CMS>

Here is the solution of my problem i finally got this:

private void exportToXmlFile(List<Device> list)
        {
            XDocument xdoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"));
            XElement elm = new XElement("CMS");
            bool flag = false;
            XElement hostDev = new XElement("Device");
            hostDev.Add(new XAttribute("Name", list.ElementAt(0).Name)); 
            hostDev.Add(new XAttribute("Type", list.ElementAt(0).TBType1));
            elm.Add(hostDev);
            xdoc.Add(elm);
            foreach (Device device in deviceToAddList)
            {
                if (device.Name != "CM_HOST")
                {
                    if(device.DeviceConnectedTo=="A")
                    {
                       if (device.ConnectedBy == "Directly")
                        {
                            flag = true;
                            XElement selectedElement = xdoc.Descendants()
                            .Where(x => (string)x.Attribute("Name") == device.ParentName).FirstOrDefault();
                            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);
                            selectedElement.Add(portAelem);
                        }
                       if(device.ConnectedBy == "MiniBot")
                        {
                            flag = true;
                            XElement selectedElement = xdoc.Descendants()
                            .Where(x => (string)x.Attribute("Name") == device.ParentName).FirstOrDefault();
                            XElement deviceElem2 = new XElement("Device");
                            XElement portAelem2 = new XElement("PortA");
                            portAelem2.Add(new XAttribute("Connected_BY", device.ConnectedBy));
                            deviceElem2.Add(new XAttribute("TB", device.TBType1));
                            deviceElem2.Add(new XAttribute("ParentConnectedToPort", device.ParentConnectedTo));
                            deviceElem2.Add(new XAttribute("MiniBot_minus_pin", device.MinusPin1));
                            deviceElem2.Add(new XAttribute("MiniBot_pluse_pin", device.PlusPin));
                            deviceElem2.Add(new XAttribute("FTDI_Port", device.FtdiPort));
                            deviceElem2.Add(new XAttribute("Name", device.Name));
                            deviceElem2.Add(new XAttribute("Cable", device.Cable));
                            portAelem2.Add(deviceElem2);
                            selectedElement.Add(portAelem2);
                        }

                    }
                    if(device.DeviceConnectedTo=="B")
                    {
                        if (device.ConnectedBy == "Directly")
                        {
                            flag = true;
                            XElement selectedElement = xdoc.Descendants()
                            .Where(x => (string)x.Attribute("Name") == device.ParentName).FirstOrDefault();
                            XElement deviceElem = new XElement("Device");
                            XElement portBelem = new XElement("PortB");
                            portBelem.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));
                            portBelem.Add(deviceElem);
                            selectedElement.Add(portBelem);
                        }
                        if (device.ConnectedBy == "MiniBot")
                        {
                            flag = true;
                            XElement selectedElement = xdoc.Descendants()
                            .Where(x => (string)x.Attribute("Name") == device.ParentName).FirstOrDefault();
                            XElement deviceElem2 = new XElement("Device");
                            XElement portBelem2 = new XElement("PortB");
                            portBelem2.Add(new XAttribute("Connected_BY", device.ConnectedBy));
                            deviceElem2.Add(new XAttribute("TB", device.TBType1));
                            deviceElem2.Add(new XAttribute("ParentConnectedToPort", device.ParentConnectedTo));
                            deviceElem2.Add(new XAttribute("MiniBot_minus_pin", device.MinusPin1));
                            deviceElem2.Add(new XAttribute("MiniBot_pluse_pin", device.PlusPin));
                            deviceElem2.Add(new XAttribute("FTDI_Port", device.FtdiPort));
                            deviceElem2.Add(new XAttribute("Name", device.Name));
                            deviceElem2.Add(new XAttribute("Cable", device.Cable));
                            portBelem2.Add(deviceElem2);
                            selectedElement.Add(portBelem2);
                        }
                    }
                }
            }

            if (flag == true)
            {
                if (cmbPowerMode.Text == "PowerSplitter" || cmbPowerMode.Text == "None")
                {

                    SX sx = new SX();
                    elm.Add(new XElement("Sx", new XAttribute("FTDI_port", numircFTDIPin.Value), new XAttribute("SX_power_button_pin", numircPowerPin.Value), new XAttribute("SX_SLP_S3_pin", numircs3Pin.Value), new XAttribute("SX_SLP_S4_pin", numircs4Pin.Value), new XAttribute("SX_SLP_S5_pin", numircs5Pin.Value), new XAttribute("SX_TBT_wake_N_pin", numircWakeNpin.Value), new XAttribute("SX_PCIE_wake_pin", numircWakePin.Value), new XAttribute("G3_Power_Mode", cmbPowerMode.Text)));

                }
                else
                {
                    MessageBox.Show("Please select Power mode value");
                }

                xdoc.Save(Application.StartupPath + "\\Topology.xml");
                MessageBox.Show("XML created !!!");
                Application.Exit();
            }
            else
                MessageBox.Show("You did not papulate data to XML file please fill up your data");
        }

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