如果有不同数量的相同节点,如何使用XmlReader ReadToFollowing? (C#)

时间:2017-04-27 09:55:17

标签: c# xml xmlreader

我不知道标题是否清晰,但这是我的问题。我有一个像这样的xml文件:

<pdv id="69330005" latitude="4578181.32121" longitude="500540.118325" cp="69330" pop="R">
<adresse>80 Rue Joseph Desbois</adresse>
<ville>MEYZIEU</ville>
<ouverture debut="01:00" fin="01:00" saufjour=""/>
<services>
  <service>Station de lavage</service>
  <service>Vente de gaz domestique</service>
  <service>Automate CB</service>
</services>
<prix nom="Gazole" id="1" maj="2017-04-18 08:44:42" valeur="1.233"/>
<prix nom="SP95" id="2" maj="2017-04-18 08:44:42" valeur="1.398"/>
<prix nom="SP98" id="6" maj="2017-04-18 08:44:42" valeur="1.454"/>
</pdv>

我想在字符串中加入属性&#34; nom&#34;和&#34; valeur&#34;所有节点&#34; prix&#34;。但目前我只是设法获得第一个&#34; prix&#34;节点和他的属性,但不能得到其他人。我该怎么办?

 public string parseXML(double latitude, double longitude, float distance)
    {
        string name = "";
        float lat, lng;
        bool isIn = false;
        using (var reader = XmlReader.Create("PrixCarburants_instantane.xml"))
        {
            while (reader.Read())
            {
                if (reader.Name == "pdv" && reader.HasAttributes && reader.GetAttribute("latitude") != string.Empty && reader.GetAttribute("longitude") != string.Empty)
                {
                    lat = float.Parse(reader.GetAttribute("latitude"), CultureInfo.InvariantCulture) / 100000;
                    lng = float.Parse(reader.GetAttribute("longitude"), CultureInfo.InvariantCulture) / 100000;
                    if(DistanceTo(latitude, longitude, lat, lng)<distance) //check if the place that I am reading in the file is at a maximum distance of the point I put in parameters
                    {

                        isIn = true;
                    }
                    if (isIn)//only read "prix" node if the place is in the distance I chose
                    {
                        reader.ReadToFollowing("prix");
                        name += reader.GetAttribute("nom") + " " +reader.GetAttribute("valeur");


                        isIn= false;
                    }

                }



            } // end while
        }

        return name;

    }

顺便说一下,&#34; prix&#34;节点在0到6之间变化。

0 个答案:

没有答案