如何将xmlnodeList复制到xmlNode和xmlelement中

时间:2016-05-03 07:47:42

标签: c# asp.net xml

帮我编写代码: 1.我有一个xml文件,我想根据节点类型(Done)读取和过滤掉xmlnodeList中的节点。 2.现在,我必须在根节点之前创建一个节点(比如Board),在Board节点下我创建xmlElement,例如Board0,Board1 ..具有一些属性(Done)。 3.现在在Board0中,我想复制我的xmlnodeList(必须编写代码)。

假设,我的xml文件如下所示:

Switchboard Version="1.4">
  <Project Port="7353" WDRemotePort="11160" WatchDogSourceAddress="256" IOProtocolVersion="V3" />
  <Controls CountControls="220">
    <IOControl Type="AnalogInput"  </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
  </Controls>
</Switchboard>

现在,我希望同样的文件应如下所示:

<Switchboard Version="1.4">
  <Project Port="7353" WDRemotePort="11160" WatchDogSourceAddress="256" IOProtocolVersion="V3" />
  <Controls CountControls="4">
    <IOControl Type="AnalogInput"  </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
  </Controls>
  <Boards CountBoards="3">
  <Board0 Name="Alm_HP_RE" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None">
    <IOControl Type="DigitalInput" </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
    <IOControl Type="DigitalInput" </IOControl>
  </Board0>
  <Board1 Name="Alm_HP_RE" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None">
  <IOControl Type="AnalogInput"  </IOControl>
  </Board1>
  <Board2 Name="Alm_HP_RE" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None">
  </Board2>
  </Boards>
</Switchboard>

我编写的代码:

{
    XmlDocument hardwareDoc = new XmlDocument();
    hardwareDoc.Load(f.FullName);
    XmlNode root = hardwareDoc.DocumentElement;
    XmlElement board = hardwareDoc.CreateElement("Boards");
    root.InsertBefore(board, root.LastChild);
    //hardwareDoc.Save(f.FullName);
    XmlElement elem = hardwareDoc.CreateElement("Board0");
    root.AppendChild(elem);
    XmlAttribute attr = hardwareDoc.CreateAttribute("Name");
    attr.Value = "DigitalOutput";
    elem.Attributes.Append(attr);
    attr = hardwareDoc.CreateAttribute("BoardWidth");
    attr.Value = "1800";
    elem.Attributes.Append(attr);
    attr = hardwareDoc.CreateAttribute("BoardHeigth");
    attr.Value = "1800";
    elem.Attributes.Append(attr);
    attr = hardwareDoc.CreateAttribute("Image");
    attr.Value = "";
    elem.Attributes.Append(attr);
    attr = hardwareDoc.CreateAttribute("ImageLayout");
    attr.Value = "None";
    elem.Attributes.Append(attr);

    root.InsertBefore(elem, root.LastChild);
    //COPYING _AnalogInputList into element: 


    hardwareDoc.Save(f.FullName);
}

private static void CreateAttribute()
        {
           DirectoryInfo plcDirInfo = new DirectoryInfo(@"C:\Users\100755224\Desktop\TEST");
           FileInfo[] hardwareFiles = plcDirInfo.GetFiles("test_123.xml");

           foreach (FileInfo f in hardwareFiles)
           {
               XmlDocument hardwareDoc = new XmlDocument();
               hardwareDoc.Load(f.FullName);
               XmlNode root = hardwareDoc.DocumentElement;
               XmlNodeList iosNodeList = hardwareDoc.GetElementsByTagName("IOControl");
              break;
           }

               foreach (FileInfo f in hardwareFiles)
               {
                   XmlDocument hardwareDoc = new XmlDocument();
                   hardwareDoc.Load(f.FullName);

                   XmlNode root = hardwareDoc.DocumentElement;
                   XmlNodeList iosNodeList = hardwareDoc.GetElementsByTagName("IOControl") ;

                   foreach (XmlNode x in iosNodeList)
                   {
                       if (x.Attributes["Type"].Value.Contains("DigitalInput"))
                       {
                           if (_AnalogInputList == null)
                           {
                               _AnalogInputList = new List<XmlNode>();
                           }
                           _AnalogInputList.Add(x);
                       }
                   }
                   XmlElement board = hardwareDoc.CreateElement("Boards-Parent");
                   root.InsertAfter(board, root.LastChild);

                   XmlElement elem = hardwareDoc.CreateElement("Board0-Child");
                   board.AppendChild(elem);
                   XmlAttribute attr = hardwareDoc.CreateAttribute("Name");
                   attr.Value = "DigitalOutput";
                   elem.Attributes.Append(attr);

                   attr = hardwareDoc.CreateAttribute("BoardWidth");
                   attr.Value = "1800";
                   elem.Attributes.Append(attr);

                   attr = hardwareDoc.CreateAttribute("BoardHeigth");
                   attr.Value = "1800";
                   elem.Attributes.Append(attr);

                   attr = hardwareDoc.CreateAttribute("Image");
                   attr.Value = "";
                   elem.Attributes.Append(attr);

                   attr = hardwareDoc.CreateAttribute("ImageLayout");
                   attr.Value = "None";
                   elem.Attributes.Append(attr);


                   XmlElement iocontrol = hardwareDoc.CreateElement("IOControl");
                   elem.AppendChild(iocontrol);

                   if (_AnalogInputList != null)
                   {
                       foreach (XmlNode m in _AnalogInputList)
                       {
                           elem.AppendChild(m);
                       }
                   }

                   hardwareDoc.Save(f.FullName);
           }

      } // end of function

我的功能正常,唯一的问题是,删除原始节点并将节点复制到后面的元素中。我想在两个地方都保留节点。 我搜索并发现需要克隆Node。 要克隆哪个节点?怎么样?

1 个答案:

答案 0 :(得分:0)

这只是第三个问题的答案[“现在在Board0中,我想复制我的xmlnodeList”]

  

如何将过滤的XMlNodeList复制到Board0?

1.使用XPATH选择所需节点

var nodeList = hardwareDoc.SelectNodes("//IOControl[@Type='DigitalInput']");

2.使用foreach循环将选定的nodeList添加到board0 XmlElement

foreach (XmlNode node in nodeList)
{
   elem.AppendChild(node);
}