Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将多个字符串指定给唯一字符串C#MultiMap、Dictionary或其他方法?_C#_String_Dictionary - Fatal编程技术网

将多个字符串指定给唯一字符串C#MultiMap、Dictionary或其他方法?

将多个字符串指定给唯一字符串C#MultiMap、Dictionary或其他方法?,c#,string,dictionary,C#,String,Dictionary,目前我遇到的问题是,我目前有两个字符串,其中包含可用于将它们彼此匹配的数据。此数据是IP源地址和IP目标地址。 我要做的是将IP源与它的所有目标地址进行匹配。我目前拥有的代码如下所示 var xmlDoc2 = new XmlDocument(); xmlDoc2.Load(textBox1.Text); HashSet<string> hs = new HashSet<string>(StringComparer.OrdinalIgnoreCase); var

目前我遇到的问题是,我目前有两个字符串,其中包含可用于将它们彼此匹配的数据。此数据是IP源地址和IP目标地址。 我要做的是将IP源与它的所有目标地址进行匹配。我目前拥有的代码如下所示

 var xmlDoc2 = new XmlDocument();
 xmlDoc2.Load(textBox1.Text);
 HashSet<string> hs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
 var node = xmlDoc2.SelectNodes("pdml/packet/proto[@name='ip']/@showname");

   foreach (XmlAttribute attribute1 in node)
   {
     string ip = attribute1.Value;
     var arr = ip.Split(); var src = arr[5]; var dst = arr[8];
     if (hs.Contains(src))
     {
      string ipsrc = src;
      string ipdst = dst;
      listBoxDST.Items.Add(ipdst);
     }
     else
     {
      hs.Add(src);
      string ipdst = dst;
      string ipsrc = src;
      listBoxSRC.Items.Add(ipsrc);
      listBoxDST.Items.Add(ipdst);
      }
   }
var xmlDoc2=new XmlDocument();
xmlDoc2.Load(textBox1.Text);
HashSet hs=新的HashSet(StringComparer.OrdinalIgnoreCase);
var node=xmlDoc2.SelectNodes(“pdml/packet/proto[@name='ip']/@showname”);
foreach(节点中的XmlAttribute attribute1)
{
字符串ip=attribute1.Value;
var arr=ip.Split();var src=arr[5];var dst=arr[8];
如果(hs.Contains(src))
{
字符串ipsrc=src;
字符串ipdst=dst;
listBoxDST.Items.Add(ipdst);
}
其他的
{
hs.Add(src);
字符串ipdst=dst;
字符串ipsrc=src;
listBoxSRC.Items.Add(ipsrc);
listBoxDST.Items.Add(ipdst);
}
}
我已经研究过多重映射,但它只在C++中使用(同样,如果我错了,请纠正我)

我还研究了字典,是否最好将ipdst存储为列表并将其添加到键(ipsrc)中。由于hashset的if语句,我似乎不知道如何使它以这种方式工作。(上面的代码没有字典的失败实现

如果我在Access中创建一个数据库用作存储会更好吗?或者是否有某种方法可以让我拥有一个唯一的源地址和一个目标地址列表

谢谢,
Tom

我实现了一个
字典
版本的代码

试试这个:

var xmlDoc2 = new XmlDocument();
xmlDoc2.Load(textBox1.Text);
Dictionary<string, List<string>> hs = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);

var node = xmlDoc2.SelectNodes("pdml/packet/proto[@name='ip']/@showname");

foreach (XmlAttribute attribute1 in node)
{
    string ip = attribute1.Value;
    var arr = ip.Split(); var src = arr[5]; var dst = arr[8];

    List<string> l;
    if (!hs.TryGetValue(src, out l))
    {
        hs[src] = l = new List<string>();
    }

    l.Add(dst);
}
var xmlDoc2=new XmlDocument();
xmlDoc2.Load(textBox1.Text);
Dictionary hs=新字典(StringComparer.OrdinalIgnoreCase);
var node=xmlDoc2.SelectNodes(“pdml/packet/proto[@name='ip']/@showname”);
foreach(节点中的XmlAttribute attribute1)
{
字符串ip=attribute1.Value;
var arr=ip.Split();var src=arr[5];var dst=arr[8];
清单l;
如果(!hs.TryGetValue(src,out l))
{
hs[src]=l=新列表();
}
l、 添加(dst);
}
它使用
TryGetValue
检查字典中是否已经有密钥。如果ip地址不存在,它会添加一个列表