本文介绍了从复杂的

问题描述

我正在处理一个具有非常嵌套结构的

现在,我需要获取所有 ID 的列表,以便当我添加新的 ConsumableAssigned 时,我可以生成 (并确保它是唯一的)新 ID.

这是我加载文件的方式:

 String savefile = Settings.Default.SelectedSaveFilePath;var 

现在,在 /Root/Stuff/Item[@Class='Consumable'] 级别,我很适合获取 但是在 /Root/Stuff/Item[@Class='Construct'] 处有一个嵌套,需要某种 sub-for-each 来获取

我正在尝试生成整个文档中所有 节点的列表,如果可能,生成一个 键值集合,其中 是值,元素( 所属)作为 Key.

我也知道 在文档中是唯一的,无论它们的父级如何.所以我可以通过搜索 ID 直接找到任何元素.

我希望这更有意义.

谢谢.

解决方案

所以你需要获取所有的 Id,当你想添加新项目时,你需要检查是否存在具有给定 id 的项目?

>

var idList = XDocument.Load("path").后代(ID").Select(x => (string)x);if(idList.Contains(givenId)){...}

或者使用 HashSet 来加快搜索速度:

 var hashSet = new HashSet(idList);if(hashSet.Contains(givenId)){...}

I'm dealing with a

<?

Now, I need to get a list of all ID so that when I add a new Consumable or Assigned, I can generate (and make sure it's a unique) new Id.

Here is how I'm loading the file:

    String savefile = Settings.Default.SelectedSaveFilePath;

    var 

Now, at the level of /Root/Stuff/Item[@Class='Consumable'], I'm good for getting the <ID> but there is a nesting at /Root/Stuff/Item[@Class='Construct'] that require sort of sub-for-each to get the <ID> of the <Assigned Class="Consumable">

I'm trying to generate a list of all the <ID> nodes in the whole document and if possible, a Collection of Key-Value where <ID> is value and Element (of whom <ID> belongs to) as Key.

I also know that the <ID> are unique in the document regardless of their parent. So I can directly find any element by searching just the ID.

I hope this make more sense.

Thanks.

解决方案

So you need to get all Id's, and when you want to add new item you need to check if there is an item exist with the given id ?

var idList = XDocument.Load("path")
                      .Descendants("ID")
                      .Select(x => (string)x);

if(idList.Contains(givenId))
{
    ...
}

Or use a HashSet for faster search:

 var hashSet = new HashSet<string>(idList);
 if(hashSet.Contains(givenId))
 {
    ...
 }

这篇关于从复杂的

08-26 09:20
查看更多