Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML解析为平面结构_C#_Xml_Struct - Fatal编程技术网

C# 将XML解析为平面结构

C# 将XML解析为平面结构,c#,xml,struct,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>

我有一个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>
我想创建此结构的数组,以便在PLC[0]中有
plc1
节点,在PLC[1]中有
plc2

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

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


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
列出PLC=doc.Root.Elements(){
ipAddress=(字符串)x.Element(“ip”),
article=(int)x.article.FirstOrDefault(),
prod=(int)x.subjects()。其中(y=>y.Name.LocalName.StartsWith(“prod”)).FirstOrDefault(),
registerId=(int)x.subjections(“registerId”).FirstOrDefault(),
registerDescription=(int)x.substands(“registerDescription”).FirstOrDefault(),
registerTarget=(int)x.subjects(“registerTarget”).FirstOrDefault()
}).ToList();
}
}
公共类公司
{
公共字符串ipAddress{get;set;}
公共int项目{get;set;}
公共int prod{get;set;}
public int registerId{get;set;}
public int registerDescription{get;set;}
公共整数寄存器目标{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>

192.168.0.170
1000
100
2000
2100
3100
192.168.0.171
1000
200
2000
2100
3200
192.168.0.172
1000
300
2000
2100
3300
试试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; }
    }


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
列出PLC=doc.Root.Elements(){
ipAddress=(字符串)x.Element(“ip”),
article=(int)x.article.FirstOrDefault(),
prod=(int)x.subjects()。其中(y=>y.Name.LocalName.StartsWith(“prod”)).FirstOrDefault(),
registerId=(int)x.subjections(“registerId”).FirstOrDefault(),
registerDescription=(int)x.substands(“registerDescription”).FirstOrDefault(),
registerTarget=(int)x.subjects(“registerTarget”).FirstOrDefault()
}).ToList();
}
}
公共类公司
{
公共字符串ipAddress{get;set;}
公共int项目{get;set;}
公共int prod{get;set;}
public int registerId{get;set;}
public int registerDescription{get;set;}
公共整数寄存器目标{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>

192.168.0.170
1000
100
2000
2100
3100
192.168.0.171
1000
200
2000
2100
3200
192.168.0.172
1000
300
2000
2100
3300

您似乎在要求某人为您编写一些代码。堆栈溢出是一个问答网站,而不是代码编写服务。请学习如何编写有效的问题。非常奇怪的XML:
您尝试过什么吗?你遇到问题了吗?也许您应该询问如何使序列化程序(XmlSerializer、DataContractSerializer、XDocument等)使用字段而不是属性?这种配置是特定于序列化程序的。@PanagiotisKanavos,感谢您的回复,我已经尝试了Linq to Xml和XmlDocument,但我无法使它们按预期工作。您似乎要求有人为您编写一些代码。堆栈溢出是一个问答网站,而不是代码编写服务。请学习如何编写有效的问题。非常奇怪的XML:
您尝试过什么吗?你遇到问题了吗?也许您应该询问如何使序列化程序(XmlSerializer、DataContractSerializer、XDocument等)使用字段而不是属性?这种配置是特定于序列化程序的。@PanagiotisKanavos,感谢您的回复,我尝试了Linq to Xml和XmlDocument,但无法使它们按预期工作。我最终发现了错误,我的“查询”错误地从节点获取了值。感谢您的努力,您的解决方案工作得非常完美。我终于找到了错误,我的“查询”错误地从节点获取了值。感谢您的努力,您的解决方案非常有效。