简体   繁体   English

c#从对象列表写入XML文件

[英]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. 我在使用XDocument和XElement写入xml文件时遇到问题,这是错误的输出文件以及应如何输出。 this is how the final xml file look like but it does not meet the requirements. 这是最终xml文件的外观,但不符合要求。

<?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 : 这是包含所有属性的Device类:

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. 无需查看您在XML文件中手动将Elements和Attributes写入哪里的代码。 Why don't you just use XML Serialization? 您为什么不只使用XML序列化? 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: 以这种方式构建所需的结构很容易,然后像这样序列化XML:

            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必须如下所示:

<?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");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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