我这里有一个名为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>
答案 0 :(得分:2)
如果没有理由不解析XML,我会将其加载到$results = $connection->query($mysql_syntax);
if ($results->num_rows > 0) {
while($row = $results->fetch_assoc()) {
if($row['type'] == 'comment'){
COMMENT HTML CODE HERE
}elseif($row['type'] == 'post'){
POST HTML CODE HERE
}
}
}
并使用这样的基本Text='<%# "Mr " + Eval("FirstName") + " " + Eval("LastName")%>'
:
XDocument
如果数据不是有效的xml,则无效。
答案 1 :(得分:1)
您的循环交互有点偏。
试试这个(包含一些测试数据,方便时删除)。
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;
}
答案 2 :(得分:0)
这是使用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();
}
答案 3 :(得分:0)
我会尝试使用RegEx和Matches采用不同的方法。问题是,它可能不是最优雅的解决方案。
我做了一些研究,发现this。在那里解释了如何在两个已知标签之间检索字符串。
以下是检索街道名称的功能:
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;
}
我认为会是这样的。我不完全确定语法,因为我不在我开发的计算机中,所以我没有机会亲自检查代码。但这可能是一个开始。如果您有任何错误或其他原因,请告诉我。