I’ve developed code like this:
String resultString = await response.Content.ReadAsStringAsync();
Stream resultStream = await response.Content.ReadAsStreamAsync();
XElement rootElement = XDocument.Load(resultStream).Elements().First();
XElement blobs = rootElement.Element("Blobs");
foreach (var blob in blobs.Elements("Blob"))
{
var t = blob;
}
Now the resultString and resultStream come from HttpClient reposnse. I get the response from an Azure blob REST service (listing), like this:
<EnumerationResults>
<Blobs>
<Blob>
<Name></Name>
<Url></Url>
<Properties></Properties>
</Blob>
<Blob>
<Name></Name>
<Url></Url>
<Properties></Properties>
</Blob>
</Blobs>
</EnumerationResults>
With my code, I’ve managed to get an IEnumerable<XNode> from <Blobs> BUT I can’t get to Name and Url inside the <Blob> element. I get all of it as a string in one line. How do I need to change my code to get each <Blob> and get from it <Name> and <Url>?
Well to start with, once you’ve loaded the document, you can just use the
Rootproperty to get at the root element. You can then get eachBlobelement usingDescendantsor via multiple calls toElements. You can do that outside theforeachloop though:or
var doc = XDocument.Load(resultStream);
var allBlobs = doc.Root.Descendants(“Blob”);
Now when you iterate over each blob, you can just get the name element using
Elementagain, and get the text content of the element by using the explicit string conversionObviously that can be done as a single statement – I just wanted to keep them separate for clarity. You can then do the same for the URL. Using the string conversion gives you a
nullreference if the element is missing, whereas using theValueproperty will give you aNullReferenceExceptionunless you guard against it. Which is more appropriate depends on your use case.An alternative approach is to do all the extraction outside your
foreachloop, in a query:Then: