【问题标题】:Parse XML to a flat struct将 XML 解析为平面结构
【发布时间】:2019-02-17 00:46:50
【问题描述】:

我有一个如下的 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 等。

我怎样才能做到这一点?提前感谢您的建议。

【问题讨论】:

  • 您似乎在要求某人为您编写一些代码。 Stack Overflow 是一个问答网站,而不是代码编写服务。请see here学习如何写出有效的问题。
  • 真的很奇怪的 XML:&lt;plc1&gt;&lt;plc2&gt;&lt;plc3&gt;
  • 你试过了吗?你遇到问题了吗?也许您应该问如何使您的序列化程序(XmlSerializer、DataContractSerializer、XDocument 等等???)使用字段而不是属性?这种配置是特定于序列化程序的
  • @PanagiotisKanavos,感谢您的回复,我已经尝试过 Linq to Xml 和 XmlDocument,但无法让它们按预期工作。

标签: c# xml struct


【解决方案1】:

试试 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>

【讨论】:

  • 我终于找到了我的错误,我有'查询'从节点错误地获取值。感谢您的努力,您的解决方案完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多