簡體   English   中英

通過Xdoc進行迭代會降低添加Xelements的能力

[英]Iterating through Xdoc decendends adding Xelements

我正在嘗試制作一個項目經理程序,以幫助組織我的項目文件。 它使用Xdocs來存儲項目信息。

我的問題是,現在我希望包括與該項目關聯的文件的更結構化視圖。 完成后應該看起來像這樣(xml的文件部分)

<files count="0">
  <folder foldername="Doks">
    <folder foldername="moks">
      <folder foldername="Floks">
        <doc>
          <fileType>doc</fileType>
          <filePath>G:\Doks\moks\Floks</filePath>
          <fileName>Dok1.doc</fileName>
          <fileID>0</fileID>
        </doc>
      </folder>
    </folder>
    <folder foldername="goks">
      <folder foldername="Floks">
        <doc>
          <fileType>doc</fileType>
          <filePath>G:\Doks\moks\Floks</filePath>
          <fileName>Dok1.doc</fileName>
          <fileID>0</fileID>
        </doc>
      </folder>
    </folder>
  </folder>
</files>

如您所見,主文件夾是doks,它具有兩個sup文件夾以及子文件夾和文件,它們都是為測試而創建的。

到目前為止,我的代碼可以找到已經存在的路徑,但是無法添加缺少的最后一部分。

原因是我使XML與現實相似,並且我已經使GUI成為系統的一部分,並且像魅力一樣工作。 而且很容易閱讀

這是代碼。

// Folder is contains the path the file e.i.
//      <folder foldername="Doks">
//        <folder foldername="moks">
//          <folder foldername="Floks"/>
//          </folder>
//      </folder>
// the file han the info about that 
// the xdoc to insert/anex the file and folder too is FilesInProject that is a public xdocument property 

private void AnexFileinXdoc(XElement file, XElement folder)
    {
        // is there  even a folde to consider
        if (folder != null)
        {
            // folder desendens list, used by looping through it  to se how menny of the folder already exists
            XElement[] list = new XElement[1];
            // handle for only on folder
            if (folder.Elements().Count() > 0)
            {
                list = folder.DescendantsAndSelf().ToArray();
            }
            else
            {
                list[0] = folder;
            }
            // debug info ignore
            // XElement[] test = FilesInProject.Root.DescendantsAndSelf("folder").ToArray();
            // list of the folderes already found this was to insure that when the loop resets and checks for the nex folder i will not flag the previous as not created.. 
            List<XElement> foundFolders = new List<XElement>();
            for (int i = 0; i < list.Length; i++)
            {
                if (FilesInProject.Root.Elements().Count() > 0)
                {
                    foreach (XElement xelem in FilesInProject.Root.Elements("folder"))
                    {
                        if (xelem.FirstAttribute.Value == list[i].FirstAttribute.Value)
                        {
                            foundFolders.Add(xelem);
                            break;
                        }
                        else
                        {
                            if (!foundFolders.Contains(xelem))
                            {
                                list[i].DescendantsAndSelf().Last().Add(file);
                                xelem.Add(list[i]);// <-- here is the problem
                            }
                            else if (i == list.Length-1)
                            {
                               xelem.Add(file); //<-- here is the problem 
                            }
                        }
                    }
                 }
            }
        }
        else
        {
            FilesInProject.Root.Add(file); 
        }
    }

我所期望的是:當我指導所有的后人找到將文件夾結構添加到xelement時,如果找到它,我可以僅在該元素(xelem)上調用add,而FilesinProject xdoc將被更新,可悲的是不這樣做,我就無法在這里找到任何東西。

我需要一種快速簡單的方法來將兩個結構合並在一起,這樣就不會有任何重復

找到了解決方案,並認為我應該在這里分享

在其他區域

    if (!foundFolders.Contains(xelem))
    {
       list[i].DescendantsAndSelf().Last().Add(file);
       xelem.Add(list[i]);// <-- here is the problem
    }
    else if (i == list.Length-1)
    {
       xelem.Add(file); //<-- here is the problem 
    }

我要做的就是將xelem.Add()更改為這樣的查詢

if (!foundFolders.Contains(xelem))
{
    list[i].DescendantsAndSelf().Last().Add(file);
    FilesInProject.Descendants("folder")
       .Where(item => item.Attribute("foldername").Value == xelem.FirstAttribute.Value).FirstOrDefault()
       .Add(list[i]);
}
else if (i == list.Length-1)
{
    FilesInProject.Descendants("folder")
       .Where(item => item.Attribute("foldername").Value == xelem.FirstAttribute.Value).FirstOrDefault()
       .Add(file);
}

它完美地工作:)

希望它也能幫助別人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM