1

既存の 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>
4

1 に答える 1

1

パラメータを指定して、ドキュメント要素の作成を処理する別のプライベート関数を作成します。元:

private xmlelement dothework(string param1, string param2){

    'do all necessary work to set up the element in here and then return it

}

このように作業を分離し、作業のセクションごとに異なる関数を作成して、最終的にはループしてそれぞれをドキュメントに追加できるようにします。

于 2013-02-11T19:07:59.643 に答える