在C#Windows窗体中读取XML Webfile并将数据放在标签中

时间:2015-03-20 19:48:44

标签: c# xml-parsing

对于我的信息程序,我需要阅读一个web xml文件并将其放在每个值的标签中。

XML文件的一个示例:

<ActueleVertrekTijden>
      <VertrekkendeTrein>
        <RitNummer>5070</RitNummer>
        <VertrekTijd>2015-03-20T19:42:00+0100</VertrekTijd>
        <EindBestemming>Den Haag Centraal</EindBestemming>
        <TreinSoort>Sprinter</TreinSoort>
        <RouteTekst>Lage Zwaluwe, Dordrecht, Rotterdam C.</RouteTekst>
        <Vervoerder>NS</Vervoerder>
        <VertrekSpoor wijziging="false">6</VertrekSpoor>
     </VertrekkendeTrein>
     <VertrekkendeTrein>
        <RitNummer>1971</RitNummer>
        <VertrekTijd>2015-03-20T19:50:00+0100</VertrekTijd>
        <EindBestemming>Venlo</EindBestemming>
        <TreinSoort>Intercity</TreinSoort>
        <RouteTekst>Tilburg, Eindhoven, Helmond</RouteTekst>
        <Vervoerder>NS</Vervoerder>
        <VertrekSpoor wijziging="false">4</VertrekSpoor>
        <Opmerkingen> // This is not always available, but it is important and specific for a 'VertrekkendeTrein'
          <Opmerking>Rijdt vandaag niet</Opmerking>
            </Opmerkingen>
        </VertrekkendeTrein>
</ActueleVertrekTijden>

我尝试使用XMLDocument(使用foreach),如下所示:

 foreach (XmlNode nodelist2 in nodeList)
            {
                if (i < 1) //1
                {

                    switch (nodelist.Name)
                    {
                        case "VertrekTijd": string vertrek1 = (nodelist2.InnerText); break;
                        case "VertrekVertragingsTekst": string vertraging1 = (nodelist2.InnerText); break;
                        case "EindBestemming": string eindbest1 = (nodelist2.InnerText); break;
                        case "TreinSoort": string treinsoort1 = (nodelist2.InnerText); break;
                        case "RouteTekst": string route1 = (nodelist2.InnerText); break;
                        case "VertrekSpoor": string spoor1 = (nodelist2.InnerText); i++ break;
                        case "Opmerkingen": case "Opmerking": string opmerking1 = (nodelist2.InnerText); break;

                    }


                }

            }

但它并没有成功。 有没有更聪明的方法来阅读它,并把它放在很多标签?

编辑: 根据我得到的答案,我尝试了以下代码:

try
        {
            string urlo = "**secured webaddress that not end with .xml**";
            string resultje = HttpGeto(urlo);
            XmlDocument doc = new XmlDocument();

            XmlNode root = doc.FirstChild;
            //* the document has one root element "ActueleVertrekTijden"
            //* the root element has multiple child nodes "VertrekkendeTrein" 
            XmlNodeList nodelist1 = root.ChildNodes;
            for (int i = 0; i < nodelist1.Count; i++)
            {
                XmlNodeList nodelist2 = nodelist1[i].ChildNodes;
                //* for each child node get all of all of child nodes,
                //* that is where you need to get the text within each one of them
                foreach (XmlNode node in nodelist2)
                {
                    switch (node.Name)
                    {
                        case "VertrekTijd":
                            string vertrek1 = node.InnerText; MessageBox.Show(vertrek1); lblts1.Text = vertrek1;
                            break;
                        case "VertrekVertragingsTekst":
                            string vertraging1 = node.InnerText;
                            break;
                        case "EindBestemming":
                            string eindbest1 = node.InnerText;
                            break;
                        case "TreinSoort":
                            string treinsoort1 = node.InnerText;
                            break;
                        case "RouteTekst":
                            string route1 = node.InnerText;
                            break;
                        case "VertrekSpoor":
                            string spoor1 = node.InnerText;
                            break;
                        case "Opmerkingen":
                            XmlNode OpNode = node.FirstChild;
                            if (OpNode != null)
                            {
                                string opmerking1 = OpNode.InnerText;
                            }
                            break;
                    }
                }
            }


        }
        catch
        {
            lblcatch.Text = "werktniet";           
        }

但它不起作用。而且,我怎样才能阅读下一个&#34; VertrekkendeTrein&#34;?,只需复制代码并使用I ++;?我需要阅读前6&#34; VertrekkendeTrein&#34;。

1 个答案:

答案 0 :(得分:0)

XmlNode root = doc.FirstChild;
//* the document has one root element "ActueleVertrekTijden"
//* the root element has multiple child nodes "VertrekkendeTrein" 
XmlNodeList nodelist1 = root.ChildNodes;
for (int i=0; i<nodelist1.Count; i++)
{
  XmlNodeList nodelist2 = nodelist1[i].ChildNodes;
  //* for each child node get all of all of child nodes,
  //* that is where you need to get the text within each one of them
  foreach (XmlNode node in nodeList2)
  {
    switch (node.Name)
    {
            case "VertrekTijd": 
  string vertrek1 = node.InnerText; 
  break;
  case "VertrekVertragingsTekst": 
  string vertraging1 = node.InnerText; 
  break;
  case "EindBestemming": 
  string eindbest1 = node.InnerText; 
  break;
  case "TreinSoort": 
  string treinsoort1 = node.InnerText; 
  break;
  case "RouteTekst": 
  string route1 = node.InnerText; 
  break;
  case "VertrekSpoor": 
  string spoor1 = node.InnerText; 
  break;
  case "Opmerkingen": 
  XmlNode OpNode = node.FirstChild;
  if(OpNode!=null)
  {
    string opmerking1 = OpNode.InnerText; 
  }
  break;
  }
  }
}