2016-03-26 38 views
1

我有一個字符串,基本上包含一個XML文件,包含標籤和所有內容。 我特別感興趣的一個標籤是<address source>。 問題是,XML文件並不總是相同的。有時可能只有一個標籤<address source>存在,有時可能有5個存在,有時甚至有20個或更多。查找字符串中的每個索引並分別存儲索引

所以,想象一下我的字符串是這樣的:

string XMLToAnalyze = "<XML><TAG1>somecontent</TAG1><address source>content</address source><address source>content</address source><TAG2>morecontent</TAG2></XML>"` 

所以,在這個特殊的字符串,還有的標籤<address source>兩次。

我需要什麼,是這樣的:

我需要找到每個標籤<address source>的索引(或的IndexOf),我需要這些索引分開儲存,優選地在單獨的整數真正(每個索引一個整數),或者在陣列中。這是因爲我需要訪問每個獨立的整數來填寫Winforms表單中的一些字段。

這可能嗎?

+1

的XML所述柱wrong.In節點<地址源>,是** **源的屬性? – kranthiv

+0

您提及的XML方式無效。 '

'是一個具有空的'source'屬性的'Address'節點。你可能希望得到消息來源來解決這個可怕的疏忽。 –

+1

您確定不會寧願將XML當作......好嗎,XML? –

回答

1

你需要的是一個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 
    } 
} 
+0

我完全不熟悉字典。有沒有可能向我展示一個如何與我的字符串結合使用的例子? – SomebodyWithAQuestion

1

使用正則表達式來匹配所有的字符串,然後遍歷匹配並找到每個匹配的索引。

這個邏輯必須工作。這一直是Tested

 List<int> indexes = new List<int>(); 
     string XMLToAnalyze = "<XML><TAG1>somecontent</TAG1><address source>content</address source><address source>content</address source><TAG2>morecontent</TAG2></XML>"; 
     var regex = new Regex(@"<address source>"); 

     foreach (Match match in regex.Matches(XMLToAnalyze)) 
     { 
      indexes.Add(match.Index); 
     } 

indexes將匹配字符串的所有索引。


輸出: 29,69

+0

謝謝,這是我熟悉的代碼。一個問題,但是,我將如何顯示「索引」內的文本框? – SomebodyWithAQuestion

+0

你使用Webforms嗎? MVC?或者是其他東西?你在哪裏有文本框 –

+0

Windows窗體就是我正在使用的。但我想我已經發現它了:我使用'string.join(environment.newline,indexes)'在多行字段中顯示索引。然而,另一個問題是:對列表來說比較新:例如,我將如何提取列表中的第二項? – SomebodyWithAQuestion