我这里有一个名为OutputKSZ的可变字符串,其中包含XML文件的代码。此XML文件的代码将包含可变数量的标签<streetName language = "EN">,后跟完全可变的街道名称,然后是</streetName>

现在,我还有一个winforms微型应用程序,它带有一个文本框,一个按钮和一个组合框。在文本框中,我复制粘贴XML代码。然后单击按钮,组合框应为我提供每个<streetName language = "EN"></streetName>标记之间所有不同街道名称的列表。

因此,为清楚起见,这里有两个可变的东西:


标签streetName的出现次数
每个streetName标记之间的每个字符串的长度。


到目前为止,这是我尝试过的:

if (OutputKSZ.Contains("<address source=\""))
{
    // LIJST MET START INDEXES
    List<int> indexesStart = new List<int>();
    var AddressSourceStart = new Regex("<streetName language=\"EN\">");
    foreach (Match match in AddressSourceStart.Matches(OutputKSZ))
    { indexesStart.Add(match.Index); }

    // LIJST MET END INDEXES
    List<int> indexesEnd = new List<int>();
    var AddressSourceEnd = new Regex("</streetName>");
    foreach (Match match in AddressSourceEnd.Matches(OutputKSZ))
    { indexesEnd.Add(match.Index); }

    int[] counterI = Enumerable.Range(0, indexesStart.Count).ToArray();
    foreach (int i in counterI)
    {
        int KSZGedeelteStraatStart = indexesStart[i];
        int KSZGedeelteStraatEnd = indexesEnd[i];
        int KSZGedeelteStraatLength = KSZGedeelteStraatEnd - KSZGedeelteStraatStart - 26;
        string KSZGedeelteStraat = OutputKSZ.Substring(KSZGedeelteStraatStart + 26, KSZGedeelteStraatLength);
        foreach (int ListCounter in counterI)
        {
            List<string> ListKSZGedeelteStraat = new List<string>();
            ListKSZGedeelteStraat.Add(KSZGedeelteStraat);
            comboBox2.DataSource = ListKSZGedeelteStraat;
        }

    }


很抱歉在那里的荷兰人。 ;)
这段代码的问题在于,它仅显示最后一次出现的情况,我真的很新鲜,我已经花了几个小时了。

你们对如何更正此有任何想法吗?我对C#还是比较陌生,所以只要保留文本框,按钮和组合框,就可以重写我的整个代码。

样本XML:

<soapenv:Envelope>
  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
  <soapenv:Body>
    <external:searchPersonInformationHistoryBySsinResponse>
      <informationCustomer>
        <customerIdentification>
          <sector>15</sector>
          <institution>5</institution>
        </customerIdentification>
      </informationCustomer>
      <informationCBSS>
        <ticketCBSS>b2d07603-2205-4258-b3b9-49320ab4b919</ticketCBSS>
        <timestampReceive>2016-03-27T12:49:59.680Z</timestampReceive>
        <timestampReply>2016-03-27T12:50:00.072Z</timestampReply>
      </informationCBSS>
      <legalContext>NISSE:IDENTIFICATION</legalContext>
      <criteria>
        <ssin>somenumber</ssin>
        <datagroups>
          <addresses>true</addresses>
        </datagroups>
      </criteria>
      <status>
        <value>DATA_FOUND</value>
        <code>MSG00000</code>
        <description>Successful</description>
      </status>
      <result>
        <person register="RR">
          <ssin>somenumber</ssin>
          <addresses status="DATA_FOUND">
            <address source="NR">
              <residentialAddress>
                <countryCode>150</countryCode>
                <countryName language="FR">Belgique</countryName>
                <countryName language="NL">België</countryName>
                <countryName language="DE">Belgien</countryName>
                <cityCode>somecitycode</cityCode>
                <cityName language="NL">somecityname</cityName>
                <postalCode>somepostalcode</postalCode>
                <streetCode>somestreetcode</streetCode>
                <streetName language="NL">somestreetname</streetName>
                <houseNumber>2</houseNumber>
                <inceptionDate>2014-08-09</inceptionDate>
              </residentialAddress>
            </address>
            <address source="NR">
              <residentialAddress>
                <countryCode>150</countryCode>
                <countryName language="FR">Belgique</countryName>
                <countryName language="NL">België</countryName>
                <countryName language="DE">Belgien</countryName>
                <cityCode>someothercitycode</cityCode>
                <cityName language="NL">someothercityname</cityName>
                <postalCode>someotherpostalcode</postalCode>
                <streetCode>someotherstreetcode</streetCode>
                <streetName language="NL">someotherstreetname</streetName>
                <houseNumber>2</houseNumber>
                <inceptionDate>2014-08-09</inceptionDate>
              </residentialAddress>
            </address>
          </addresses>
        </person>
      </result>
    </external:searchPersonInformationHistoryBySsinResponse>
  </soapenv:Body>
</soapenv:Envelope>

最佳答案

您的循环迭代有些偏离。
试试看(包括一些测试数据,请在方便时删除)。

string OutputKSZ = "<address source=\">" +
                                "<streetName language=\"EN\">1</streetName> " +
                                "<streetName language=\"EN\">12</streetName> " +
                                "<streetName language=\"EN\">111</streetName> "
                                ;

            //Moved for scoping purposes
            List<string> ListKSZGedeelteStraat = new List<string>();

            if (OutputKSZ.Contains("<address source=\""))
            {
                // LIJST MET START INDEXES
                List<int> indexesStart = new List<int>();
                var AddressSourceStart = new Regex("<streetName language=\"EN\">");
                foreach (Match match in AddressSourceStart.Matches(OutputKSZ))
                {
                    indexesStart.Add(match.Index);
                }

                // LIJST MET END INDEXES
                List<int> indexesEnd = new List<int>();
                var AddressSourceEnd = new Regex("</streetName>");
                foreach (Match match in AddressSourceEnd.Matches(OutputKSZ))
                {
                    indexesEnd.Add(match.Index);
                }

                int[] counterI = Enumerable.Range(0, indexesStart.Count).ToArray();
                foreach (int i in counterI)
                {
                    int KSZGedeelteStraatStart = indexesStart[i];
                    int KSZGedeelteStraatEnd = indexesEnd[i];
                    int KSZGedeelteStraatLength = KSZGedeelteStraatEnd - KSZGedeelteStraatStart - 26;
                    string KSZGedeelteStraat = OutputKSZ.Substring(KSZGedeelteStraatStart + 26, KSZGedeelteStraatLength);

                    //Remove additional foreach loop - you were adding too many times
                    ListKSZGedeelteStraat.Add(KSZGedeelteStraat);
                }

                //Assign data source once
                comboBox2.DataSource = ListKSZGedeelteStraat;
            }

关于c# - C#列出组合框中变量字符串中的所有匹配项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36360071/

10-11 23:51