XML元素我訪問:就在這裏,如果下面的示例是不可讀: http://api.petfinder.com/pet.find?&animal=dog&count=150&location=90605&key=a94884c2349b5b6975ec84fb51ac3ec4linq到XML。我如何訪問這些數據?
<pets>
<pet>
<id>19575609</id>
<shelterId>CA699</shelterId> <shelterPetId/>
<name>Louie</name>
<animal>Dog</animal>
<breeds>
<breed>Basset Hound</breed>
</breeds>
<mix>no</mix>
<age>Adult</age>
<sex>M</sex>
<size>M</size>
<options> ... </options>
<description>
<![CDATA[<div>Hi, my name is Louie. I'm a great looking houndthat really really wants his next home to be his final loving one. I'm looking for an adult home that will have patience with me as I learn to trust humans again. I like other dogs and tolerate cats. I've got lovely big front feet - just like a Basset should have. I walk well on a leash, and I'm house-trained. I'm a shy boy but I warm up once I get to know you and with lots of love and patience I'm sure in no time I'll turn into a hound that is a happy-go-lucky boy. I'm up to date on all vaccines and my adoption fee is $200.00. Please consider meeting me - I think once you do you will want to take me home and love me for the rest of my life. </div> <br/><a href="http://www.petfinder.com/petdetail/19575609">View this pet on Petfinder.com</a> ]]>
</description>
<lastUpdate>2011-05-16T19:22:23Z</lastUpdate>
<status>A</status>
<media>
<photos>
<photo size="x" id="1">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-1-x.jpg</photo>
<photo size="t" id="1">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-1-t.jpg</photo>
<photo size="pn" id="1">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-1-pn.jpg</photo>
<photo size="pnt" id="1">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-1-pnt.jpg</photo>
<photo size="fpm" id="1">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-1-fpm.jpg</photo>
<photo size="x" id="2">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-2-x.jpg</photo>
<photo size="t" id="2">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-2-t.jpg</photo>
<photo size="pn" id="2">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-2-pn.jpg</photo>
<photo size="pnt" id="2">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-2-pnt.jpg</photo>
<photo size="fpm" id="2">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-2-fpm.jpg</photo>
<photo size="x" id="3">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-3-x.jpg</photo>
<photo size="t" id="3">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-3-t.jpg</photo>
<photo size="pn" id="3">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-3-pn.jpg</photo>
<photo size="pnt" id="3">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-3-pnt.jpg</photo>
<photo size="fpm" id="3">http://photocache.petfinder.com/fotos/CA699/CA699.19575609-3-fpm.jpg</photo>
</photos>
</media>
</pet>
</pets>
我想使用的代碼如下。我試圖返回的屬性是url。
void processXML(XElement _xml)
{
XNamespace ns = "";
var pets = from item in
_xml.Elements(ns + "pets").Elements(ns + "pet")
select item;
foreach (var pet in pets)
{
Pet _pet = new Pet();
_pet.id = (string)pet.Element(ns + "id");
_pet.breeds = (string)pet.Element(ns + "breeds").Element(ns + "breed");
_pet.name = (string)pet.Element(ns + "name");
_pet.animal = (string)pet.Element(ns + "animal");
**_pet.media = (string)pet.Element(ns + "media")
.Element(ns + "photos").Element(ns + "photo")
.Attribute(ns + "fpm");**
Results.Items.Add(_pet);
}
}
在此先感謝您的任何指導。
豐富
有什麼問題了嗎?什麼不起作用?你得到什麼錯誤信息? – 2011-05-30 07:42:11
供將來參考:不要從瀏覽器視圖中剪切和粘貼XML,最好將其保存爲文本文件並從那裏複製。並使用代碼格式。 – Richard 2011-05-30 07:42:24
你是問題:我如何獲得大小屬性爲「fpm」的photo元素(其URL)的值? – Richard 2011-05-30 07:45:12