none
LInq and XML RRS feed

  • Question

  • I have the following XML form which is coming back form Amazon S3 bucket list and I want to use linq to query the Bucket names but because it the second level down it seems to be failing to find the Name attribute 

    <?xml version="1.0" encoding="UTF-8"?>
    <ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Owner>
    <ID>345hh42ih423iu4h23iu4h32iu4h32iu4h</ID>
    <DisplayName>MyName</DisplayName>
    </Owner>
    <Buckets>
    <Bucket>
    <Name>Bucket1</Name>
    <CreationDate>2015-04-25T19:20:37.000Z</CreationDate>
    </Bucket>
    <Bucket>
    <Name>Bucket2</Name>
    <CreationDate>2015-05-18T13:00:52.000Z</CreationDate>
    </Bucket>
    <Bucket>
    
    </Buckets>


    the code i have is 

                XDocument xdoc = XDocument.Parse(XMLFeed) ;
    
                var query = from t in xdoc.Descendants("Name")
                            select t.Value;


    but this fails to find the anything?


    Regards Dagz

    Friday, August 4, 2017 4:24 PM

Answers

  • I think that the behavior is caused by the namespace. Remove temporarily the xmlns attribute and your code will work.

    You will find several solutions to make it work with namespaces. Try this too:

    var query = from t in xdoc.Descendants( "{http://s3.amazonaws.com/doc/2006-03-01/}Name" )
                select t.Value;
    

    • Marked as answer by Dagz Monday, August 7, 2017 7:57 AM
    Monday, August 7, 2017 4:42 AM