我如何阅读这个XML部分
本文关键字:XML 部分 何阅读 | 更新日期: 2023-09-27 18:09:46
我有一个在线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"中列出的"Opmerking",并站在"VertrekkendeTrein"中:
<Opmerkingen>
<Opmerking> Rijdt vandaag niet</Opmerking>
</Opmerkingen>
问题是它并不总是存在的,这要视情况而定。
我可以读取其他部分,如"RitNummer"answers"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
有什么方法可以让我"上班"吗?
谢谢!
最简单但不是最优雅的解决方案是从"Opmerkingen"节点列表中提取子节点:
case "Opmerkingen":
{
XmlNodeList nodeListOp = nodelist2.SelectNodes("Opmerking");
foreach (XmlNode node in nodeListOp)
{
if (node.Name == "Opmerking")
{
// your "Opmerking" stuff here
}
}
break;
}
您可以尝试将XML节点列表/集转换为可查询的数据结构,并使用System.Linq
查看它是否包含和Opmerking。虽然这是我的偏好,因为我一直在做很多数据库查询。
在您的情况下,对于您的switch
语句和许多情况,您可以尝试以下操作:
在"Opmerkingen"的情况下,嵌套另一个情况,看看它是否有一个子节点"Opmerking",然后对该"Opmerking"节点做一些事情。或者让case "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
去nodelist的下一个节点。同样,关键是在看到Opmerkingen时调用该方法这样就可以获得Opmerkingen