简体   繁体   English

通过Xdoc进行迭代会降低添加Xelements的能力

[英]Iterating through Xdoc decendends adding Xelements

I'm trying to make a project manager program, to help organize my project files. 我正在尝试制作一个项目经理程序,以帮助组织我的项目文件。 It uses Xdocs to store the project information. 它使用Xdocs来存储项目信息。

My problem is that now I wish to include a more structured view of the files associated with the project. 我的问题是,现在我希望包括与该项目关联的文件的更结构化视图。 That should look like this when it is done (file part of the xml) 完成后应该看起来像这样(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>

As you can see the main folder is doks that has two sup folders with sub folders and files it is all created for tests of course. 如您所见,主文件夹是doks,它具有两个sup文件夹以及子文件夹和文件,它们都是为测试而创建的。

So far my code can find the already existing path, but cannot add the last part that is missing. 到目前为止,我的代码可以找到已经存在的路径,但是无法添加缺少的最后一部分。

The reason for this is that I what the XML to resemble the reality, and that I have already made the GUI part of the system and it Works like a charm. 原因是我使XML与现实相似,并且我已经使GUI成为系统的一部分,并且像魅力一样工作。 Also it is easy to read 而且很容易阅读

Here is the code. 这是代码。

// 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); 
        }
    }

What I expected was that: When I foreached through the decedents to find were to add my folder structure to the xelement that if I found it I could just call add on that element(xelem) and the FilesinProject xdoc would be updated, sadly it do not do this and I haven't been able to find anything here on the matter. 我所期望的是:当我指导所有的后人找到将文件夹结构添加到xelement时,如果找到它,我可以仅在该元素(xelem)上调用add,而FilesinProject xdoc将被更新,可悲的是不这样做,我就无法在这里找到任何东西。

I need a quick and simple way to merge the two structures together so I don't get any duplicates 我需要一种快速简单的方法来将两个结构合并在一起,这样就不会有任何重复

Found the solution and thought I should share it here 找到了解决方案,并认为我应该在这里分享

In the Else block 在其他区域

    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 
    }

all I had to do was to change xelem.Add() to a query like this 我要做的就是将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);
}

and it works perfectly :) 它完美地工作:)

Hope it helps others too 希望它也能帮助别人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM