你需要的是一個IDictionary < [string],IList < [int] >>。 在搜索開始標記時,可以按照以下方式將它們存儲在字典中: 如果標記存在,則將新找到的索引添加到添加的列表中,否則將新的元素添加到字典中並添加一個新列表其中的一個項目 - 索引的第一次出現。在你完成所有的字符串之後 - 你的字典將會包含你正在尋找的地圖。
public static class XmlTagMapBuilder
{
public static IDictionary<string, IList<int>> GetOpenTagIndexMap(string inputXml)
{
// Argument validation goes here
IDictionary<string, IList<int>> result = new Dictionary<string, IList<int>>();
int currentIndex = -1;
string lastOpenTag = null;
while (true)
{
string nextOpenTagName;
int nextOpenTagIndex;
if (TryGetNextOpenTagIndex(inputXml, currentIndex, out nextOpenTagName, out nextOpenTagIndex))
{
lastOpenTag = nextOpenTagName;
currentIndex = nextOpenTagIndex;
IList<int> tagIndicies;
if (!result.TryGetValue(nextOpenTagName.ToUpperInvariant(), out tagIndicies))
{
tagIndicies = new List<int>();
result.Add(nextOpenTagName, tagIndicies);
}
tagIndicies.Add(nextOpenTagIndex);
}
else
{
break;
}
}
return result;
}
/// <summary>
/// Tries to get next open tag in the given <see cref="inputXml"/> string after the specified startIndex.
/// </summary>
/// <param name="inputXml">The string which contains the xml tags.</param>
/// <param name="startIndex">The index after which to look for the open tag.</param>
/// <param name="nextOpenTagName">If a tag was found, contains its name.</param>
/// <param name="nextOpenTagIndex">If a tag was found, contains the start index of it.</param>
/// <returns>true - if the tag was found. false - otherwise.</returns>
private static bool TryGetNextOpenTagIndex(string inputXml, int startIndex, out string nextOpenTagName, out int nextOpenTagIndex)
{
// Need to add implementaiton here
}
}
的XML所述柱wrong.In節點<地址源>,是** **源的屬性? – kranthiv
您提及的XML方式無效。 '
'是一個具有空的'source'屬性的'Address'節點。你可能希望得到消息來源來解決這個可怕的疏忽。 –您確定不會寧願將XML當作......好嗎,XML? –