2017-09-29 6 views
3

* .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でユーザーが指定したフォルダパスには、上記のフォルダ構造を持つ複数のフォルダが含まれており、上記のフォルダ構造を持つすべてのフォルダに対してこの操作を実行します。あなたは例外を取得しているところ

+0

JapanDaveにしてみてください。私はxmlで作業する前に問題が起きていることを伝えることができます。 – Crowcoder

+0

これは、 '文字列[] xmlFilesArray1 = Directory.GetFiles(xmlDir [0]、 "* .xmlファイル")にエラーを与えている;'が、私はそれを修復する方法がわかりません。 – Bumba

+1

その前の行はディレクトリを取得していないので、 'xmlDir'は空であり、インデックスゼロを参照することはできません。 – Crowcoder

答えて

2

この

//get all directories from basepath 
     string[] filesindirectory = Directory.GetDirectories(basePath); 


     //Loop through each parent directory and get each matching xml file from it 
     List<string[]> newList = filesindirectory.Select(folder => (from item in Directory.GetDirectories(folder, "meta", SearchOption.AllDirectories) 
         .Select(item => Directory.GetFiles(item, "*.xml")) 
         .ToList() 
         .SelectMany(x => x) 
        let sx = Directory.GetDirectories(folder, "xml", SearchOption.AllDirectories) 
         .Select(items => Directory.GetFiles(items, "*.xml")) 
         .ToList() 
         .SelectMany(s => s) 
         .Any(s => Path.GetFileName(s) == Path.GetFileName(item)) 
        where sx 
        select item).ToArray() 
       .Concat((from xmlItem in Directory.GetDirectories(folder, "xml", SearchOption.AllDirectories) 
         .Select(item => Directory.GetFiles(item, "*.xml")) 
         .ToList() 
         .SelectMany(xs => xs) 
        let sx = Directory.GetDirectories(folder, "meta", SearchOption.AllDirectories) 
         .Select(items => Directory.GetFiles(items, "*.xml")) 
         .ToList() 
         .SelectMany(sc => sc) 
         .Any(sc => Path.GetFileName(sc) == Path.GetFileName(xmlItem)) 
        where sx 
        select xmlItem).ToArray())) 
      .Select(xmlFiles => xmlFiles.ToArray()).ToList(); 


     //loop through each element of the jagged array 
     foreach (string[] path in newList) 
     { 
      for (int j = 0; j < path.Length/2; j++) 
      { 
       XDocument doc = Xdocument.Load(path[j]); 
       string name = doc.Root.Element("Emp").Element("lbl").Value; 
       XDocument doc2 = Xdocument.Load(path[(path.Length/2) + j]); 
       doc2.Root.Element("Employee").SetElementValue("label", name); 
       doc2.Save(path[(path.Length/2) + j]); 
      } 
     } 

クレジットデバッグそれ

0

xmlDir[0]の最初の使用です。前の行は、あなたの条件に一致する0個のディレクトリを返さなければなりません。

デバッガでxmlDirと打ち上げを割り当てライン上のブレークポイントを設定します。線を乗り越えると、xmlDirにゼロ要素が表示されます。要素インデックス0(最初の要素)の項目にアクセスしようとしていますが、何もないので、IndexOutOfRangeExceptionが表示されます。

最初の要素にアクセスする前に、xmlDir.Length > 0またはxmlDir.Any()でチェックすることをお勧めします。

+0

私はその問題を取り除きました(私は私の投稿を更新しました)が、今私は新しいエラー** System.NullReferenceExceptionを取得しています:オブジェクト参照がオブジェクトのインスタンスに設定されていません**コード "string name = doc.Root.Element(" Employee ")。要素(" label ")。値;' – Bumba

+0

デバッガのその行にブレークポイントがあると、マウスの各プロパティにカーソルが移動します。そのうちの1人がnullと言うでしょう。それがあなたの問題です。おそらくxmlにはあなたが期待している位置に要素がありません。 –

関連する問題