使用Linq到xml选择Xml元素值

时间:2014-09-11 02:03:34

标签: c# xml linq

我正在使用enom api检查具有多个tld的域。 xml返回多个响应,但使用Linq我只找到一个。我是Linq的新手,但这对我来说很合适。任何建议

// xml

<interface-response> 
    <Domain>test.COM</Domain> 
    <RRPCode>211</RRPCode> 
    <RRPText>Domain not available</RRPText> 
    <Domain>test.NET</Domain> 
    <RRPCode>211</RRPCode> 
    <RRPText>Domain not available</RRPText> 

    <DomainCount>2</DomainCount> 
    <Command>CHECK</Command> 
    <APIType>API.NET</APIType> 
    <Language>eng</Language> 
    <ErrCount>0</ErrCount> 
    <ResponseCount>0</ResponseCount> 
    <MinPeriod>1</MinPeriod> 
    <MaxPeriod>10</MaxPeriod> 
    <Server>sjl0vwresell_t1</Server> 
    <Site>eNom</Site> 
    <IsLockable/> 
    <IsRealTimeTLD/> 
    <TimeDifference>+0.00</TimeDifference> 
    <ExecTime>0.688</ExecTime> 
    <Done>true</Done> 
    <TrackingKey>66793410-c808-4671-8039-2925ce5da78f</TrackingKey>
    <RequestDateTime>9/9/2014 6:48:07 PM</RequestDateTime> 
    <debug/> 
 </interface-response>  

//代码

//Run query
var recs = from x in xml.Descendants("interface-response")
  select new {
        Domain = (string) x.Element("Domain").Value,
    RRPCode = (string) x.Element("RRPCode").Value,
    RRPText = (string) x.Element("RRPText").Value
  };

try { 
  //Loop through results
  foreach (var d in recs) {
    result.AppendLine(d.Domain);
    result.AppendLine(d.RRPCode);
    result.AppendLine(d.RRPText);
   }
} catch (Exception ex) {
  result.AppendLine(ex.Message);
} 

有什么想法吗?

0 个答案:

没有答案