首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代Xdoc结束添加Xelements

迭代Xdoc结束添加Xelements
EN

Stack Overflow用户
提问于 2014-05-22 09:22:12
回答 1查看 59关注 0票数 0

我试图制定一个项目经理程序,以帮助组织我的项目文件。它使用Xdocs存储项目信息。

我的问题是,现在我希望包含与项目相关的文件的更结构化的视图。当它完成时应该是这样的(xml的文件部分)

代码语言:javascript
复制
<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作为系统的一部分,它的工作就像一种魅力。它也很容易读懂。

这是密码。

代码语言:javascript
复制
// 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); 
        }
    }

我所期望的是:当我预先查看要查找的样本时,将我的文件夹结构添加到xelem元素中,如果我找到它,我只需调用Addon那个元素(Xelem),FilesinProject xdoc就会被更新,遗憾的是它没有这样做,而且我在这里找不到任何东西。

我需要一种快速而简单的方法将两个结构合并在一起,这样我就不会得到任何副本

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-27 10:58:27

找到了解决方案,我想我应该在这里分享

在其他块中

代码语言:javascript
复制
    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()更改为这样的查询

代码语言:javascript
复制
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);
}

它工作得很完美:)

希望它也能帮助到其他人

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23802685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档