これは、基本的な Linq から XML へのクエリで立ち往生しているこの質問の派生物です
LINQ の記述と LINQ to XML についての学習が上達するように努めています。LINQ クエリは期待どおりの結果を返しますが、コードはきれいに見えません。もっと良い書き方はないでしょうか?
XML
<ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK">
<Errors/>
<Warnings/>
<RequestedCommand>namecheap.domains.check</RequestedCommand>
<CommandResponse>
<DomainCheckResult Domain="gooaagle.com" Available="true"/>
</CommandResponse>
<Server>WEB1-SANDBOX1</Server>
<GMTTimeDifference>--4:00</GMTTimeDifference>
<ExecutionTime>0.859</ExecutionTime>
</ApiResponse>
C#
XNamespace ns = "http://api.namecheap.com/xml.response";
var response = (
from r in doc.Elements()
select new
{
Errors = r.Element(ns + "Errors").Value,
Warnings = r.Element(ns + "Warnings").Value,
RequestedCommand = r.Element(ns + "RequestedCommand").Value,
CommandResponse = new
{
Domain= r.Element(ns + "CommandResponse").Element(ns + "DomainCheckResult").Attribute("Domain"),
Available = r.Element(ns + "CommandResponse").Element(ns + "DomainCheckResult").Attribute("Available")
},
Server = r.Element(ns + "Server").Value
});