using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace OperationXml { class Program { static void Main(string[] args) { LoadConfig("PLCParameter.xml"); Console.ReadKey(); } public static void LoadConfig(string filename) { try { XDocument doc = XDocument.Load(filename); XElement PLC = doc.Element("root").Element("HardwareDevices").Element("HardwareDevice").Element("PLC"); InitBlocks(PLC.Element("IOs").Element("DirectIO").Element("Blocks")); } catch (Exception ex) { throw ex; } } private static void InitBlocks(XElement Blocks) { var blocks = from p in Blocks.Elements("Block") select p; //Console.WriteLine(blocks.ElementAt(0)); //Console.WriteLine(blocks.ElementAt(1)); //Console.WriteLine(blocks.ElementAt(2)); //Console.WriteLine(blocks.ElementAt(3)); //Console.WriteLine(blocks.ElementAt(4)); //Console.WriteLine(blocks.ElementAt(5)); //Console.WriteLine(blocks.ElementAt(6)); //ele=block foreach (var ele in blocks) { Console.WriteLine($"{ele}"); } } } }
<?xml version="1.0" encoding="utf-8"?> <root xsi:noNamespaceSchemaLocation="SystemParameter.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <HardwareDevices> <HardwareDevice DeviceID="0" Name="CPU1" DeviceType="PLC"> <PLC> <Communication CommType="Network"> <!-- <Network Address="172.27.224.33.1.1" Port="851" Simulation="false"/> --> <Network Address="172.27.224.33.1.1" Port="5025" Simulation="True"/> </Communication> <Protocol ProtocolType="AdsProtocol"> <MCProtocol ConnectUnitNumber="0" NetworkNumber="3" SourceNetworkNumber="1023" SourceStationNumber="0" StationNumber="0"/> <AdsProtocol ConnectUnitNumber="0" NetworkNumber="3" SourceNetworkNumber="1023" SourceStationNumber="0" StationNumber="0"/> </Protocol> <IOs> <DirectIO DataOffset="0" DataLen="11748"> <Blocks> <Block ID="0" Type="0" Offset="0" Number="84" BaseOffset="0" DB="1"/> <Block ID="1" Type="1" Offset="84" Number="1248" BaseOffset="0" DB="1"/> <Block ID="2" Type="2" Offset="8192" Number="104" BaseOffset="0" DB="1"/> <Block ID="3" Type="3" Offset="8296" Number="3448" BaseOffset="0" DB="1"/> <Block ID="3287" Type="5" Offset="4096" Number="12024" BaseOffset="0" DB="1" VarName="PLC_EPD_DATA.EPD_TORQUE_DATA"/> <Block ID="3281" Type="7" Offset="2096" Number="24024" BaseOffset="0" DB="1" VarName="PLC_EPD_DATA.EPD_LASER_DATA"/> <Block ID="3283" Type="9" Offset="2048" Number="48024" BaseOffset="0" DB="1" VarName="PLC_EPD_DATA.EPD_ECV_DATA"/> </Blocks> </DirectIO> </IOs> <Product> <Type>"QSerial"</Type> <Manufacture>"Mitsubishi"</Manufacture> </Product> </PLC> </HardwareDevice> </HardwareDevices> </root>
输出结果:
<Block ID="0" Type="0" Offset="0" Number="84" BaseOffset="0" DB="1" /> <Block ID="1" Type="1" Offset="84" Number="1248" BaseOffset="0" DB="1" /> <Block ID="2" Type="2" Offset="8192" Number="104" BaseOffset="0" DB="1" /> <Block ID="3" Type="3" Offset="8296" Number="3448" BaseOffset="0" DB="1" /> <Block ID="3287" Type="5" Offset="4096" Number="12024" BaseOffset="0" DB="1" VarName="PLC_EPD_DATA.EPD_TORQUE_DATA" /> <Block ID="3281" Type="7" Offset="2096" Number="24024" BaseOffset="0" DB="1" VarName="PLC_EPD_DATA.EPD_LASER_DATA" /> <Block ID="3283" Type="9" Offset="2048" Number="48024" BaseOffset="0" DB="1" VarName="PLC_EPD_DATA.EPD_ECV_DATA" />