Search code examples
c#xmlamazon-web-servicesamazon-s3xelement

Read the file count in a public AWS S3 bucket


I am able to retrieve the XML which contains the info about the files using the below piece of code:

XElement responseXML = XElement.Load(AWS_S3_URL);        
ICollection<XElement> fileElements = responseXML.Elements("Contents") as ICollection<XElement>;
Debug.Log("responseXML.Elements() ===== " + fileElements.ToString());

The first line of code returns the following xml. But I am not able to retrieve the count of element:

<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Name>XXXXXXXXXXXX</Name>
    <Prefix />
    <Marker />
    <MaxKeys>1000</MaxKeys>
    <IsTruncated>false</IsTruncated>
    <Contents>
        <Key>AffectivaLogs/</Key>
        <LastModified>2018-04-16T00:20:06.000Z</LastModified>
        <ETag>"d41d8cd98f00b204e9800998ecf8427e"</ETag>
        <Size>0</Size>
        <Owner>
            <ID>65a011a29cdf8ec533ec3d1ccaae921c</ID>
        </Owner>
        <StorageClass>STANDARD</StorageClass>
    </Contents>
    <Contents>
        <Key>Bean Man with Affectiva.Editor.csproj</Key>
        <LastModified>2018-04-16T05:40:43.000Z</LastModified>
        <ETag>"7ea67a5fa37ceaeaa90ef3dbebddb201"</ETag>
        <Size>34349</Size>
        <StorageClass>STANDARD</StorageClass>
    </Contents>
</ListBucketResult>

Solution

  • Able to do it easily using XmlDocument instead of XElement.

    XmlDocument responseXML = new XmlDocument();
    responseXML.Load(AWS_S3_URL);
    XmlElement root = responseXML.DocumentElement;
    XmlNodeList node = root.GetElementsByTagName("Contents");        
    Debug.Log("responseXML.Elements() ===== " + node.Count);