链接到 XML。我如何访问这些数据?

发布于 2024-11-10 10:17:33 字数 4970 浏览 2 评论 0原文

我正在访问的 XML 元素:位于此处,如果以下示例不可读: http://api.petfinder.com/pet.find?&animal=dog&count=150&location=90605&key=a94884c2349b5b6975ec84fb51ac3ec4

<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&#39;m a great looking houndthat      really really wants his next home to be his final loving one. I&#39;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&#39;ve got lovely big front feet - just like a Basset should have. I walk well on a leash, and I&#39;m house-trained. I&#39;m a shy boy but I warm up once I get to know you and with lots of love and patience I&#39;m sure in no time I&#39;ll turn into a hound that is a happy-go-lucky boy. I&#39;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);

        }
    }

预先感谢您的任何指导。

富有的

XML element I am accessing: is here, if below sample is unreadable:
http://api.petfinder.com/pet.find?&animal=dog&count=150&location=90605&key=a94884c2349b5b6975ec84fb51ac3ec4

<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>

The code I am trying to use is below. the attribute I am trying to return is the 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);

        }
    }

Thanks in advance for any guidance.

rich

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2024-11-17 10:17:33

如果我理解你必须做这样的事情:

XmlElement photoList=pet.Element(ns + "media").Element(ns + "photos");

var url = from i in photoList.Descendants("photo")
    where i.attribute("size").Equals("fpm")
    select i.value;

此刻我无法编译它,但我认为这是一个很好的起点!

已更新

 void processXML(XElement _xml)
    {


        var pets = from item in
                       _xml.Elements("pet")
                   select item;

        foreach (var pet in pets)
        {
            //Pet _pet = new Pet();

             string id = (string)pet.Element("id");
             string breeds = (string)pet.Element("breeds").Element("breed");
             string name = (string)pet.Element("name");
             string animal = (string)pet.Element("animal");

             /*string media = (string)pet.Element(ns + "media")
                                                        .Element(ns + "photos").Element(ns + "photo")
                                                        .Attribute(ns + "fpm");*/



             var url = from i in pet.Descendants().Elements("photo")
                       where i.Attribute("size").Value.Equals("fpm")
                       select i.Value;
             //Results.Items.Add(_pet);

        }
    }

If I understand you have to do something like this:

XmlElement photoList=pet.Element(ns + "media").Element(ns + "photos");

var url = from i in photoList.Descendants("photo")
    where i.attribute("size").Equals("fpm")
    select i.value;

In this moment I cannot compile it but I think it is a good starting point!

updated

 void processXML(XElement _xml)
    {


        var pets = from item in
                       _xml.Elements("pet")
                   select item;

        foreach (var pet in pets)
        {
            //Pet _pet = new Pet();

             string id = (string)pet.Element("id");
             string breeds = (string)pet.Element("breeds").Element("breed");
             string name = (string)pet.Element("name");
             string animal = (string)pet.Element("animal");

             /*string media = (string)pet.Element(ns + "media")
                                                        .Element(ns + "photos").Element(ns + "photo")
                                                        .Attribute(ns + "fpm");*/



             var url = from i in pet.Descendants().Elements("photo")
                       where i.Attribute("size").Value.Equals("fpm")
                       select i.Value;
             //Results.Items.Add(_pet);

        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文