附加Xml给定次数

本文关键字:Xml 附加 | 更新日期: 2023-09-27 17:58:33

我正在附加到一个现有的xml文件:代码如下:我正在使用C#.net 4.5 VS 2012并创建一个WPF应用程序。

我怎么能把这个附加30次,然后只把D100属性编号改为2,3,4,5等等?其他值相同!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
namespace AppendX
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("C:''Temp.xml");
            XmlNamespaceManager namespaces = new XmlNamespaceManager(doc.NameTable);
            namespaces.AddNamespace("flp", "http://www.w3.org/2001/flp");

            XmlNode nextNode = doc.SelectSingleNode("/flp:Tab/flp:Designs", namespaces);
            XmlElement D100 = doc.CreateElement("flp", "D100", "http://www.w3.org/2001/flp");
            D100.SetAttribute("Number", "2");
            XmlElement Code = doc.CreateElement("flp", "Code", "http://www.w3.org/2001/flp");
            Code.InnerText = "B";
            D100.AppendChild(Code);
            XmlElement Documented = doc.CreateElement("flp", "Documented", "http://www.w3.org/2001/flp");
            Documented.InnerText = "false";
            D100.AppendChild(Documented);
            nextNode.AppendChild(D100);
            doc.Save("test1.xml");

        }
    }
}

这是我正在使用的示例xml,很抱歉我本想把它挂起来的!

<flp:Tab xmlns:flp="http://www.w3.org/2001/flp"   Title="Testing">
  <flp:Form Number="0" id="1005" />
  <flp:Rev Time="2013-01-21T15:08:00">
    <flp:Author Name="Brad" Aid="15" />
  </flp:Rev>
  <flp:Designs Id="D100">
    <flp:D100 Number="1">
      <flp:Code>A</flp:Code>
      <flp:Documented>true</flp:Documented>
    </flp:D100>
  </flp:Designs>
</flp:Tab>

附加Xml给定次数

在给定参数的情况下,创建一个单独的私有函数来处理文档元素的创建。例如:

private xmlelement dothework(string param1, string param2){
    'do all necessary work to set up the element in here and then return it
}

我会像这样将工作分开,为每个工作部分创建不同的函数,这样最终您就可以循环并将每个函数附加到文档中。