将XML解析为平面结构

时间:2018-09-12 11:30:52

标签: c# xml struct

我有一个XML文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <plc1>
        <ip>192.168.0.170</ip>
        <regread>
            <article>1000</article>
            <prod1>100</prod1>
        </regread>
        <regwrite>
            <registerId>2000</registerId>
            <registerDescription>2100</registerDescription>
            <registerTarget>3100</registerTarget>
        </regwrite>
    </plc1>
    <plc2>
        <ip>192.168.0.171</ip>
        <regread>
            <article>1000</article>
            <prod1>200</prod1>
        </regread>
        <regwrite>
            <registerId>2000</registerId>
            <registerDescription>2100</registerDescription>
            <registerTarget>3200</registerTarget>
        </regwrite>
    </plc2>
    <plc3>
        <ip>192.168.0.172</ip>
        <regread>
            <article>1000</article>
            <prod>300</prod>
        </regread>
        <regwrite>
            <registerId>2000</registerId>
            <registerDescription>2100</registerDescription>
            <registerTarget>3300</registerTarget>
        </regwrite>
    </plc3>
</root>

我必须将这些节点的值存储到这样的C#结构中:

public struct PLC
{
    public string ipAddress;
    public int article;
    public int prod;
    public int registerId;
    public int registerDescription;
    public int registerTarget;
}

我想创建一个此结构的数组,以便在PLC [0]中将有plc1个节点,在PLC [1]中将有plc2等。

我该如何实现?预先感谢您的建议。

1 个答案:

答案 0 :(得分:3)

尝试xml linq:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<PLC> plcs = doc.Root.Elements().Select(x => new PLC() {
                ipAddress = (string)x.Element("ip"),
                article = (int)x.Descendants("article").FirstOrDefault(),
                prod = (int)x.Descendants().Where(y => y.Name.LocalName.StartsWith("prod")).FirstOrDefault(),
                registerId = (int)x.Descendants("registerId").FirstOrDefault(),
                registerDescription = (int)x.Descendants("registerDescription").FirstOrDefault(),
                registerTarget = (int)x.Descendants("registerTarget").FirstOrDefault()
            }).ToList();


        }
    }
    public class PLC
    {
        public string ipAddress { get; set; }
        public int article { get; set; }
        public int prod { get; set; }
        public int registerId { get; set; }
        public int registerDescription { get; set; }
        public int registerTarget { get; set; }
    }


}

这是我使用的xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
<plc1>

  <ip>192.168.0.170</ip>

  <regread>
    <article>1000</article>
    <prod1>100</prod1>
  </regread>

  <regwrite>
    <registerId>2000</registerId>
    <registerDescription>2100</registerDescription>
    <registerTarget>3100</registerTarget>
  </regwrite>

</plc1>

<plc2>

  <ip>192.168.0.171</ip>

  <regread>
    <article>1000</article>
    <prod1>200</prod1>
  </regread>

  <regwrite>
    <registerId>2000</registerId>
    <registerDescription>2100</registerDescription>
    <registerTarget>3200</registerTarget>
  </regwrite>

</plc2>

<plc3>

  <ip>192.168.0.172</ip>

  <regread>
    <article>1000</article>
    <prod>300</prod>
  </regread>

  <regwrite>
    <registerId>2000</registerId>
    <registerDescription>2100</registerDescription>
    <registerTarget>3300</registerTarget>
  </regwrite>

</plc3>
</root>