* .xmlファイルのノード内容を別のファイルから更新しようとしています。 フォルダ構造は、私がどのxmlファイルの最初のノードラベルの内容によってXMLフォルダ内にあるXMLファイルの最初のノードラベルを変更したいフォームFolder_structureC#の別のファイルからノードの内容を更新していますか?
でありますメタフォルダ内にあります。 以下は、私が
using System;
using System.IO;
using System.Xml.Linq;
using System.Linq;
using System.Windows.Forms;
namespace XML_Parse
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
TextBox1.Text = folderBrowserDialog1.SelectedPath;
}
}
void Button3Click(object sender, EventArgs e)
{
string targetDirectory1 = TextBox1.Text;
string[] xmlDir = Directory.GetDirectories([email protected]"\", "xml*", SearchOption.AllDirectories);
string[] xmlFilesArray1 = Directory.GetFiles(xmlDir[0], "*.xml", SearchOption.AllDirectories);
string[] xmlDir2 = Directory.GetDirectories([email protected]"\", "meta*", SearchOption.AllDirectories);
string[] xmlFilesArray2 = Directory.GetFiles(xmlDir[0], "*.xml", SearchOption.AllDirectories);
foreach (string xmlFile in xmlFilesArray1)
{
var FileInfo1 = new FileInfo(xmlFile);
string FileLocation1 = FileInfo1.FullName;
string file_name = Path.GetFileName(FileLocation1);
foreach (var xmlFile2 in xmlFilesArray2)
{
if (xmlFile2.Contains(file_name))
{
string path = Path.GetFullPath(xmlFile2);
XDocument doc = XDocument.Load(path);
string name = doc.Root.Element("Employee").Element("label").Value;
XDocument doc2 = XDocument.Load(FileLocation1);
doc2.Root.Element("Employee").SetElementValue("label", name);
doc2.Save(FileLocation1);
}
}
}
MessageBox.Show("Process complete");
}
}
}
を試みた。しかし、私はエラーメッセージ
System.IndexOutOfRangeExceptionを取得していたコードです:インデックスが配列の境界外です。 注:TextBox1でユーザーが指定したフォルダパスには、上記のフォルダ構造を持つ複数のフォルダが含まれており、上記のフォルダ構造を持つすべてのフォルダに対してこの操作を実行します。あなたは例外を取得しているところ
JapanDaveにしてみてください。私はxmlで作業する前に問題が起きていることを伝えることができます。 – Crowcoder
これは、 '文字列[] xmlFilesArray1 = Directory.GetFiles(xmlDir [0]、 "* .xmlファイル")にエラーを与えている;'が、私はそれを修復する方法がわかりません。 – Bumba
その前の行はディレクトリを取得していないので、 'xmlDir'は空であり、インデックスゼロを参照することはできません。 – Crowcoder