我如何反序列化此xml文件

时间:2019-04-12 15:51:13

标签: c# xml deserialization

我正在反序列化xml文件(我需要通过GUID对节点/对象进行排序,然后重新序列化),但是我无法正确地反序列化

我已经创建了类,但是文件未正确读取。这些类在第二段代码中列出,此外,如果监视的值标记为false,则必须将其忽略并且不能将其写入文件中。

    <?xml version="1.0" encoding="utf-8"?>
<EnvironmentNodeConfigurationParameters>
  <ComplexDeviceParameterList>
    <ComplexNodeConfigurations SubDeviceID="Controller_1" SubDeviceName="Controller_1">
  <ComplexNodeParameterList>
    <NodeConfigurations NodeID="1" NodeName="Temperature" DependNodeName="" RegisterType="HoldingRegs" StartReg="128" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="ABAABB1E-CCFE-4BF2-BBFC-90142F1B177A" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="Temperature_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="0" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="8dcc904e-1484-43d2-8bf1-3dc19f01951d" Enabled="true" Units="Celcius" Monitored="true" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="LowTemp_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="40" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="3d73a245-2766-40ca-b77a-b777af79e215" Enabled="true" Units="Celcius" Monitored="false" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="HighTemp_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="41" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="db4059d4-bb26-467c-9e36-4c755d9a5e98" Enabled="true" Units="Celcius" Monitored="false" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="LowTemp_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="2" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="fa11e66b-c269-4790-887d-0ac4bb30541d" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="HighTemp_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="4" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="0e4653bf-e43b-4725-9f00-731d5e356f12" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="Temp_Offset" DependNodeName="" RegisterType="HoldingRegs" StartReg="9" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="1d0c6fa0-6819-4b7e-8145-536954b8bdd7" Enabled="true" Units="Celcius" Monitored="false" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="PV1_Temperature" DependNodeName="" RegisterType="HoldingRegs" StartReg="134" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="b921d2a7-997b-4a32-8460-1a63177f1711" Enabled="true" Units="Celcius" Monitored="false" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="Output" DependNodeName="" RegisterType="HoldingRegs" StartReg="130" NumRegs="1" NodeType="FreezerOutput" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="c87482e3-edb5-46b9-9fdc-8915d49eeb1e" Enabled="true" Units="Percent" Monitored="true" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="132" NumRegs="1" NodeType="FreezerStatus" ValueType="System.Int32" EnvironmentLinkType="Amtn" Guid="b59cbbc5-c286-4f6c-ae99-b3a822808fc7" Enabled="true" Units="" Monitored="false" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="1" NodeName="Circuit_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="139" NumRegs="1" NodeType="FreezerStatus" ValueType="System.Int32" EnvironmentLinkType="Amtn" Guid="4f90d425-ba0b-401d-b339-795e5411a1f0" Enabled="true" Units="" Monitored="false" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
  </ComplexNodeParameterList>
</ComplexNodeConfigurations>
<ComplexNodeConfigurations SubDeviceID="Controller_2" SubDeviceName="Controller_2">
  <ComplexNodeParameterList>
    <NodeConfigurations NodeID="2" NodeName="Temperature" DependNodeName="" RegisterType="HoldingRegs" StartReg="128" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="{85F59633-FF15-4FF7-A419-8BF9B7C80FF8}" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="Temperature_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="0" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="3ed6fee4-8a63-43f4-87be-83f45f81e524" Enabled="true" Units="Celcius" Monitored="true" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="LowTemp_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="40" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="d11a0fbf-b92b-42b4-9423-9be0081c0cdf" Enabled="true" Units="Celcius" Monitored="false" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="HighTemp_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="41" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="4ecc10ff-c189-4e39-971d-dd8e0cbfddf0" Enabled="true" Units="Celcius" Monitored="false" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="LowTemp_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="2" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="1d891f0f-1ab0-487d-a5eb-77fe089240e7" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="HighTemp_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="4" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="5d54d839-4d17-43c1-82a8-14c6199b6ab2" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="Temp_Offset" DependNodeName="" RegisterType="HoldingRegs" StartReg="9" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="e6bbe973-9a94-4aeb-b104-7a3460962244" Enabled="true" Units="Celcius" Monitored="false" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="PV1_Temperature" DependNodeName="" RegisterType="HoldingRegs" StartReg="134" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="95f03a54-cb4e-48f8-93c5-e6e4ce9a1a4c" Enabled="true" Units="Celcius" Monitored="false" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="Output" DependNodeName="" RegisterType="HoldingRegs" StartReg="130" NumRegs="1" NodeType="FreezerOutput" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="04802db4-aea9-4787-97c0-3eca56c802b0" Enabled="true" Units="Percent" Monitored="true" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="132" NumRegs="1" NodeType="FreezerStatus" ValueType="System.Int32" EnvironmentLinkType="Amtn" Guid="218f7c0a-bb21-49eb-b36a-4c0ed00e1b07" Enabled="true" Units="" Monitored="false" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="2" NodeName="Circuit_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="139" NumRegs="1" NodeType="FreezerStatus" ValueType="System.Int32" EnvironmentLinkType="Amtn" Guid="aeb98aec-a688-47d3-afda-3b6bf9bb1a43" Enabled="true" Units="" Monitored="false" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
  </ComplexNodeParameterList>
</ComplexNodeConfigurations>
<ComplexNodeConfigurations SubDeviceID="Controller_3" SubDeviceName="Controller_3">
  <ComplexNodeParameterList>
    <NodeConfigurations NodeID="3" NodeName="Temperature" DependNodeName="" RegisterType="HoldingRegs" StartReg="128" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="1CD619B5-9E1B-448E-9A5B-7BFF4B480813" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="Temperature_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="0" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="97ba767c-7dd9-4c74-b3ea-fd40ff48927e" Enabled="true" Units="Celcius" Monitored="true" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="LowTemp_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="40" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="9fa1ae56-7d43-4da9-b481-09a1befee510" Enabled="true" Units="Celcius" Monitored="false" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="HighTemp_SetPoint" DependNodeName="" RegisterType="HoldingRegs" StartReg="41" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="7c9b72dc-0150-4dfd-8b72-6f79def6de7d" Enabled="true" Units="Celcius" Monitored="false" ReportItem="20">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="LowTemp_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="2" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="353117b2-dd05-4b67-a8f1-afda5570afc6" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="HighTemp_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="4" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="6a7852a4-9d29-413b-bda2-674176774a7c" Enabled="true" Units="Celcius" Monitored="true" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="Temp_Offset" DependNodeName="" RegisterType="HoldingRegs" StartReg="9" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="6660cbd6-ebbb-49cc-a622-dab1df02c263" Enabled="true" Units="Celcius" Monitored="false" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="PV1_Temperature" DependNodeName="" RegisterType="HoldingRegs" StartReg="134" NumRegs="1" NodeType="FreezerTemperature" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="186445bc-d0d4-4227-87e6-016918be1160" Enabled="true" Units="Celcius" Monitored="false" ReportItem="10">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="Output" DependNodeName="" RegisterType="HoldingRegs" StartReg="130" NumRegs="1" NodeType="FreezerOutput" ValueType="System.Double" EnvironmentLinkType="Amtn" Guid="b707e3a5-91c1-4c15-9021-e1035d605421" Enabled="true" Units="Percent" Monitored="true" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="132" NumRegs="1" NodeType="FreezerStatus" ValueType="System.Int32" EnvironmentLinkType="Amtn" Guid="d27d00ce-6696-4d92-9236-9fae182134b6" Enabled="true" Units="" Monitored="false" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
    <NodeConfigurations NodeID="3" NodeName="Circuit_Alarm" DependNodeName="" RegisterType="HoldingRegs" StartReg="139" NumRegs="1" NodeType="FreezerStatus" ValueType="System.Int32" EnvironmentLinkType="Amtn" Guid="4eacd952-3a7f-4b8e-b0d1-6054b1dfd996" Enabled="true" Units="" Monitored="false" ReportItem="0">
      <EnvironmentLinkParams>
        <KeyValuePair Key="IPAddress" Value="192.168.1.250" />
      </EnvironmentLinkParams>
    </NodeConfigurations>
  </ComplexNodeParameterList>
</ComplexNodeConfigurations>

 [Serializable()]
    [XmlRoot("EnvironmentNodeConfigurationParameters")]
    public class EnvironmentNodeConfigurationParameters
    {
        [XmlElement("ComplexDeviceParameterList")]
        ComplexDeviceParameterList cdpl = new ComplexDeviceParameterList();

    }
    [Serializable()]
    public class ComplexDeviceParameterList
    {
        [XmlElement("ComplexNodeConfigurations")]
        List<ComplexNodeConfigurations> cnc = new List<ComplexNodeConfigurations>();
    }
    [Serializable()]
    public class ComplexNodeConfigurations
    {

        [XmlElement("ComplexNodeParameterList")]
        ComplexNodeConfigurations cnc2 = new ComplexNodeConfigurations();

    }
    [Serializable()]
    public class ComplexNodeParameterList
    {
        [XmlElement("NodeConfigurations")]
        List<NodeConfigurations> nodeList = new List<NodeConfigurations>();
    }
    [Serializable()]
    public class NodeConfigurations
    {


        [XmlElement("EnvironmentLinkParams")]
        List<EnvironmentLinkParams> elplist = new List<EnvironmentLinkParams>();
    }
    [Serializable()]
    public class EnvironmentLinkParams
    {
        [XmlElement("KeyValuePair")]
        KeyValuePair kvp = new KeyValuePair();
    }
    [Serializable()]
    public class KeyValuePair
    {
        [XmlElement("NodeConfigurations")]
        List<NodeConfigurations> nodeList = new List<NodeConfigurations>();
        [XmlAttribute("Key")]
        public string key = "IPAddress";
        [XmlAttribute("Value")]
        public string value = "192.168.1.250";
    }

我需要代码来获取xml文件,然后按GUID将其反序列化,然后重新序列化。

1 个答案:

答案 0 :(得分:0)

这是我的程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication108
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            XmlSerializer serializer = new XmlSerializer(typeof(EnvironmentNodeConfigurationParameters));
            EnvironmentNodeConfigurationParameters parameters = (EnvironmentNodeConfigurationParameters)serializer.Deserialize(reader);

            List<ComplexNodeConfigurations> cncs = parameters.cdpl.cnc;

            foreach (ComplexNodeConfigurations cnc in cncs)
            {
                List<NodeConfigurations> nodeList = cnc.cnc2.nodeList;

                nodeList = nodeList.OrderBy(x => x.guid).ToList();
            }

        }
    }
    [Serializable()]
    [XmlRoot("EnvironmentNodeConfigurationParameters")]
    public class EnvironmentNodeConfigurationParameters
    {
        [XmlElement("ComplexDeviceParameterList")]
        public ComplexDeviceParameterList cdpl { get; set; }

    }
    [Serializable()]
    public class ComplexDeviceParameterList
    {
        [XmlElement("ComplexNodeConfigurations")]
        public List<ComplexNodeConfigurations> cnc { get; set; }
    }
    [Serializable()]
    public class ComplexNodeConfigurations
    {

        [XmlElement("ComplexNodeParameterList")]
        public ComplexNodeParameterList cnc2 { get; set; }

    }
    [Serializable()]
    public class ComplexNodeParameterList
    {
        [XmlElement("NodeConfigurations")]
        public List<NodeConfigurations> nodeList { get; set; }
    }
    [Serializable()]
    public class NodeConfigurations
    {
        [XmlElement("EnvironmentLinkParams")]
        public List<EnvironmentLinkParams> elplist { get; set; }
        [XmlAttribute("Guid")]
        public string guid { get; set; }

    }
    [Serializable()]
    public class EnvironmentLinkParams
    {
        [XmlElement("KeyValuePair")]
        public KP kvp { get; set; }
    }
    [Serializable()]
    public class KP
    {
        [XmlElement("NodeConfigurations")]
        public List<NodeConfigurations> nodeList { get; set; }
        [XmlAttribute("Key")]
        public string key { get; set; }
        [XmlAttribute("Value")]
        public string value { get; set; }
    }

}