Linq XML选择最后一个父级并插入元素

时间:2019-06-10 09:01:03

标签: c# xml linq

我必须构建此XML文件

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<K5project version="1.1">
   <networks>
      <modbus>
         <modbusport kind="master" address="127.0.0.1">
            <modbusreq name="" mode="Read Input Registers">
               <modbusitem ope="Data" offset="0" symbol="F1CU01_state_to_ias1" />
               <modbusitem ope="Data" offset="1" symbol="F1CU01_state_to_ias2" />
               <modbusitem ope="Data" offset="2" symbol="F1CU02_state_to_ias1" />
               <modbusitem ope="Data" offset="3" symbol="F1CU02_state_to_ias2" />
            </modbusreq>
            <modbusreq name="" mode="Read Input Registers">
               <modbusitem ope="Data" offset="0" symbol="F1CU01_state_to_ias1" />
               <modbusitem ope="Data" offset="1" symbol="F1CU01_state_to_ias2" />
            </modbusreq>
         </modbusport>
         <modbusport kind="master" address="127.0.0.2">
            <modbusreq name="" mode="Read Input Registers">
               <modbusitem ope="Data" offset="0" symbol="F1CU01_state_to_ias1" />
               <modbusitem ope="Data" offset="1" symbol="F1CU01_state_to_ias2" />
               <modbusitem ope="Data" offset="2" symbol="F1CU02_state_to_ias1" />
               <modbusitem ope="Data" offset="3" symbol="F1CU02_state_to_ias2" />
            </modbusreq>
            <modbusreq name="" mode="Read Input Registers">
               <modbusitem ope="Data" offset="0" symbol="F1CU01_state_to_ias1" />
               <modbusitem ope="Data" offset="1" symbol="F1CU01_state_to_ias2" />
            </modbusreq>
         </modbusport>
      </modbus>
   </networks>
   <fieldbus>
   </fieldbus>
</K5project>

因此,我要做的就是以这种方式创建“基本”初始模型:

private static XDocument CreateFieldBusAutoXmlBody()
        {
            XDocument fieldbusXML = new XDocument(
                new XDeclaration("1.0", "utf-16", "yes"),
                new XElement("K5Project",
                    new XAttribute("version", "1.1"),
                    new XElement("networks",
                        new XElement("modbus",
                            new XElement("modbusport",
                                new XAttribute("kind", "master"),
                                new XAttribute("address", "10.0.0." + "1")
                            )
                    )
            )));

            return fieldbusXML;
        }

因此第一个<modbus>已创建。我需要做的是每100 modbusitem我要添加一个新的modbusreq;并且每10 modbusreq我需要添加一个新的modbusport

我正在尝试选择最后一个modbusport,以在其中添加新的modbusreq;但我无法做到:我做到了

XElement elemToAddFromAutomation = fieldbusAutoXMLPac1.Descendants("modbusport").Last();

但这是错误的。

  • 如何选择最后存在的modbusport
  • 是否有关于Linq XML的选择和基础搜索的不错指南?

1 个答案:

答案 0 :(得分:1)

请创建一个新的控制台应用程序,并使用下面的代码(工作正常,请享用):

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

namespace ConsoleApp4
{
class Program
{
    static void Main(string[] args)
    {
        K5project k5Project = LoadXml("file.xml");
        var lastModbusport = k5Project.Networks.Modbus.Modbusport.Last();
        //here lastModbusport contain last Modbusport... enjoy it
    }

    private static K5project LoadXml(string filename)
    {
        var serializer = new XmlSerializer(typeof(K5project));

        if (!File.Exists(filename))
        {
            return null;
        }
        using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            return (K5project)serializer.Deserialize(fs);
        }
    }
}



[XmlRoot(ElementName = "modbusitem")]
public class Modbusitem
{
    [XmlAttribute(AttributeName = "ope")]
    public string Ope { get; set; }
    [XmlAttribute(AttributeName = "offset")]
    public string Offset { get; set; }
    [XmlAttribute(AttributeName = "symbol")]
    public string Symbol { get; set; }
}

[XmlRoot(ElementName = "modbusreq")]
public class Modbusreq
{
    [XmlElement(ElementName = "modbusitem")]
    public List<Modbusitem> Modbusitem { get; set; }
    [XmlAttribute(AttributeName = "name")]
    public string Name { get; set; }
    [XmlAttribute(AttributeName = "mode")]
    public string Mode { get; set; }
}

[XmlRoot(ElementName = "modbusport")]
public class Modbusport
{
    [XmlElement(ElementName = "modbusreq")]
    public List<Modbusreq> Modbusreq { get; set; }
    [XmlAttribute(AttributeName = "kind")]
    public string Kind { get; set; }
    [XmlAttribute(AttributeName = "address")]
    public string Address { get; set; }
}

[XmlRoot(ElementName = "modbus")]
public class Modbus
{
    [XmlElement(ElementName = "modbusport")]
    public List<Modbusport> Modbusport { get; set; }
}

[XmlRoot(ElementName = "networks")]
public class Networks
{
    [XmlElement(ElementName = "modbus")]
    public Modbus Modbus { get; set; }
}

[XmlRoot(ElementName = "K5project")]
public class K5project
{
    [XmlElement(ElementName = "networks")]
    public Networks Networks { get; set; }
    [XmlAttribute(AttributeName = "version")]
    public string Version { get; set; }
}
}

在调试模式下的结果: enter image description here