我有指向我在应用程序中读取的在线XML文件的链接。
这是XML的一部分:
<ActueleVertrekTijden>
<VertrekkendeTrein>
<RitNummer>4085</RitNummer>
<VertrekTijd>2014-06-13T22:00:00+0200</VertrekTijd>
<EindBestemming>Rotterdam Centraal</EindBestemming>
<TreinSoort>Sprinter</TreinSoort>
<RouteTekst>A'dam Sloterdijk, Amsterdam C., Duivendrecht</RouteTekst>
<Vervoerder>NS</Vervoerder>
<VertrekSpoor wijziging="false">4</VertrekSpoor>
<Opmerkingen>
<Opmerking> Rijdt vandaag niet</Opmerking>
</Opmerkingen>
</VertrekkendeTrein>
</ActueleVertrekTijden>
我想阅读“ Opmerkingen”中列出的并且位于“ VertrekkendeTrein”中的“ Opmerking”:
<Opmerkingen>
<Opmerking> Rijdt vandaag niet</Opmerking>
</Opmerkingen>
问题是它并不总是存在的,这取决于情况。
我可以阅读“ RitNummer”和“ Vervoerder”等其他部分,并使用以下代码来做到这一点:
string urlo = "THEWEBSITE";
string resultje = HttpGeto(urlo);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resultje);
XmlNodeList nodeList = xmlDoc.SelectNodes("ActueleVertrekTijden/VertrekkendeTrein/*");
foreach (XmlNode nodelist2 in nodeList)
{//14 101517
//int i = 0;//if (i == 0) { i++; }
if (i < 1) //1
{
switch (nodelist2.Name)
{
case "VertrekTijd": string kuttijd1 = (nodelist2.InnerText);
var res1 = Regex.Match(kuttijd1, @"\d{1,2}:\d{1,2}").Value;
lblv1.Text = Convert.ToString(res1); break;
case "VertrekVertragingTekst": var tobiasisnietvantexel1 = (nodelist2.InnerText); if (tobiasisnietvantexel1 == null) {} else{ ververt1.Text = tobiasisnietvantexel1; ververt1.Visible = true; vertpic1.Visible = true; logo1.Top -= 9; lblts1.Top -= 9; } break;
case "EindBestemming": string vertrek1 = (nodelist2.InnerText); if (vertrek1 == "Uitgeest") { lblvia1.Text = "Krommenie-Ass"; } lblbs1.Text = vertrek1; break;
case "TreinSoort": lblts1.Text = (nodelist2.InnerText); break;
case "RouteTekst": lblvia1.Text = (nodelist2.InnerText); break;
case "VertrekSpoor": lbls1.Text = (nodelist2.InnerText); i++; break;
}
}
}
我做了很多尝试:
case "Opmerking": var texeliseeneiland1
和
case "Opmerkingen/Opmerking": var texeliseeneiland1
有什么方法可以为“ Opmerking”辩护吗?
谢谢!
最佳答案
您可以尝试将XML节点列表/集合转换为可查询的数据结构,并使用System.Linq
来查看它是否包含和Opmerking。尽管那是我的偏爱,因为我一直在进行很多数据库查询。
在您的情况下,对于您的switch
语句以及许多情况,您可以尝试以下操作:
对于“ Opmerkingen”,嵌套另一个案例以查看其是否具有子节点“ Opmerking”,然后对该“ Opmerking”节点进行操作。或者让案例“ Opmerkingen”调用一个函数以对“ opmerking”节点进行操作。
编辑示例:
foreach (XmlNode nodelist2 in nodeList)
{//14 101517
//int i = 0;//if (i == 0) { i++; }
if (i < 1) //1
{
switch (nodelist2.Name)
{
case "VertrekTijd": string kuttijd1 = (nodelist2.InnerText);
var res1 = Regex.Match(kuttijd1, @"\d{1,2}:\d{1,2}").Value;
lblv1.Text = Convert.ToString(res1); break;
case "VertrekVertragingTekst": var tobiasisnietvantexel1 = (nodelist2.InnerText); if (tobiasisnietvantexel1 == null) {} else{ ververt1.Text = tobiasisnietvantexel1; ververt1.Visible = true; vertpic1.Visible = true; logo1.Top -= 9; lblts1.Top -= 9; } break;
case "EindBestemming": string vertrek1 = (nodelist2.InnerText); if (vertrek1 == "Uitgeest") { lblvia1.Text = "Krommenie-Ass"; } lblbs1.Text = vertrek1; break;
case "TreinSoort": lblts1.Text = (nodelist2.InnerText); break;
case "RouteTekst": lblvia1.Text = (nodelist2.InnerText); break;
case "VertrekSpoor": lbls1.Text = (nodelist2.InnerText); i++; break;
//Here's the case you might try adding
case "Opmerkingen": var SomeVar = OpmerkingenMethod(nodelist2); break;
//or do something else that you want to do, instead of breaking,
//I do not know what you want to do with Opmerking when you get it
// so I just had the function output into another variable.
}
}
}
这是在Opmerkingen情况下可能使用的方法的示例,以便您可以使用Opmerking并执行一些操作。再说一次,我不知道你到底想做什么
private string OpmerkingenMethod(XmlNode node)
{
//Do something with XmlNode Opmerkingen
var something = node.Name //Or whatever you want from Opmerkingen node
return something
}
这可能更多地符合您想要的内容,同样,我还没有测试代码,这只是为了演示满足您需求的代码。
private string OpmerkingenMethod(XmlNode node)
{
//Do something with XmlNode Opmerkingen
var child = node.FistChild;
if(child.name == "Opmerking")
{
return child.InnerText // or whatever you want
}
else { return "" ; }
}
您可以在Opmerkingen的情况下调用此方法并检查输出,如果您想要的是
break
switch
语句,如果不是您想要的(因为没有opmerking或其他东西),您可以continue
下一种情况,或break
转到节点列表中的下一个节点。同样,关键是在看到Opmerkingen时调用该方法,以便可以获取Opmerkingen内的节点或值。