[英]c# List all occurrences within variable string in combobox
我這里有一個名為OutputKSZ的可變字符串,其中包含XML文件的代碼。 該XML文件的代碼將包含可變數量的標簽<streetName language = "EN">
后跟完全可變的街道名稱,然后是</streetName>
。
現在,我還有一個winforms微型應用程序,它帶有一個文本框,一個按鈕和一個組合框。 在文本框中,我復制粘貼XML代碼。 然后,我單擊按鈕,組合框應為我提供每個<streetName language = "EN"></streetName>
標記之間所有不同街道名稱的列表。
因此,為了清楚起見,這里有兩個可變的東西:
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>
如果沒有理由不解析XML,則將其加載到XDocument
並使用基本的LINQ query
如下所示:
var OutputKSZ = textBox1.Text;
var xdocument = XDocument.Parse(OutputKSZ);
var streets = xdocument.Descendants("streetName");
var streetNames = streets.Select(s => s.Value).ToList();
comboBox2.DataSource = streetNames;
如果數據不是有效的xml,這將無法工作。
您的循環中斷了一點。
試試看(包括一些測試數據,請在方便時刪除)。
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;
}
這是使用LINQ獲得所需內容的好方法。 將其插入LinqPad進行嘗試:
void Main()
{
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Address",
new XElement("streetName", "Linden Strass",
new XAttribute("language", "EN")),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
),
new XElement("Contact",
new XElement("Name", "Max Dane"),
new XElement("Address",
new XElement("streetName", "Marten Strass",
new XAttribute("language", "EN")),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
var streets = contacts.Elements("Contact").Elements("Address").Elements("streetName")
.Where(c => (string)c.Attribute("language") == "EN").ToList();
streets.Dump();
// Another way to express this getting all elements of that name
var streets2 = contacts.Descendants("streetName")
.Where(c => (string)c.Attribute("language") == "EN").ToList();
streets2.Dump();
}
我會嘗試使用RegEx和Matches的不同方法。 問題在於,這可能不是最優雅的解決方案。
我做了一些研究,發現了這一點 。 在那里說明了如何檢索兩個已知標簽之間的字符串。
這是檢索街道名稱的功能:
List<string> extractString(string xmldata){
Regex regex = new Regex("<streetName language = \"EN\">(.*?)</streetName>");
MatchCollection streetCollection = regex.Matches(xmldata);
//Total number of matches=streetCollection.count
List<string> streetList = new List<string>();
for(int i=0;i<streetCollection.count;i++){
streetList.Add(streetCollection[ctr].Value);
}
return streetList;
}
我想那會是這樣。 由於我不在開發計算機中,因此我對語法沒有完全的把握,因此我自己沒有機會檢查代碼。 但這可能是一個開始。 告訴我您是否遇到任何錯誤或其他問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.