停留在基本的LINQ to XML查询 [英] Stuck on basic Linq to XML query

查看:117
本文介绍了停留在基本的LINQ to XML查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从中提取namecheap沙盒API的信息,也无法弄清楚,为什么我的LINQ查询不工作。



下面是一个示例响应



XML

 < ApiResponse状态=OK的xmlns =http://api.namecheap.com/xml.response> 
<错误/>
<警告/>
< RequestedCommand> namecheap.domains.check< / RequestedCommand>
< CommandResponse>
< DomainCheckResult域=google.com服务=FALSE/>
< / CommandResponse>
<服务器与GT; WEB1-SANDBOX1< /服务器>
< GMTTimeDifference> - 4:00< / GMTTimeDifference>
< ExecutionTime>&0.875 LT; / ExecutionTime>
< / ApiResponse>



C#



  VAR DOC = XDocument.Load(URL); 
变种响应从doc.Root.Descendants R(ApiResponse=(

,其中1 == 1
选择新{
错误= r.Element (错误)。价值
警告= r.Element(警告)。价值
RequestedCommand = r.Element(RequestedCommand)。价值
CommandResponse = R。 。元素(CommandResponse)值,
服务器= r.Element(服务器)值
}
)。



我也试过这个查询使用相同的文档只是为了看看一个简单的例子工作。

  VAR测试= doc.Descendants(RequestedCommand)第一()值。; 



但都返回NULL。所以我要去哪里错了?我最终会需要获取顶级元素和内CommandResponse更深的元素。与任何帮助,也将不胜感激。



更新



由于乔恩的答复中提到,这主要是与不是问题引用的各种元件时使用的名称空间。也用于doc.Elements()而不是doc.Root。后人()。



下面是一个更新的工作版本。

 的XNamespace NS =http://api.namecheap.com/xml.response; 
VAR响应=(从R
在doc.Elements()
选择新的
{
错误= r.Element(NS +错误)。价值,
警告= r.Element(NS +警告)。价值
RequestedCommand = r.Element(NS +RequestedCommand)。价值
CommandResponse = r.Element(NS + CommandResponse)值,
服务器= r.Element(NS +服务器)值
}
)。


解决方案

的问题是,你不使用命名空间当你正在寻找的元素,后人等:

 的XNamespace NS =http://api.namecheap.com/xml 。响应; 
VAR DOC = XDocument.Load(URL);
VAR响应= doc.Root
.Descendants(NS +ApiResponse)
。选择(R = gt;新建{
错误= r.Element(NS +错误).value的,

});



(请注意,您永远不需要,其中1 == 1 在LINQ ......我从查询表达式语法,因为它不会给你任何东西改变了这一点。)



该命名空间从<$继承C $ C>< ApiResponse> 元素作为所有其他元素的默认命名空间,因为它只是的xmlns = ... ,而不是指定一个别名。



另外请注意,如果你已经证明我们的全部的XML文档,那么上面不会找到任何元素,你要求 ApiReponse 元素的下面的根元素,而它的的根元素。


I'm trying to extract the information from the namecheap sandbox api and can't figure out why my linq queries aren't working.

Here's a sample response.

XML

<ApiResponse Status="OK" xmlns="http://api.namecheap.com/xml.response">
  <Errors />
  <Warnings />
  <RequestedCommand>namecheap.domains.check</RequestedCommand>
  <CommandResponse>
    <DomainCheckResult Domain="google.com" Available="false" />
  </CommandResponse>
  <Server>WEB1-SANDBOX1</Server>
  <GMTTimeDifference>--4:00</GMTTimeDifference>
  <ExecutionTime>0.875</ExecutionTime>
</ApiResponse>

C#

var doc = XDocument.Load(url);
var response = (
    from  r in doc.Root.Descendants("ApiResponse") 
    where 1==1
    select new  { 
        Errors = r.Element("Errors").Value,
        Warnings = r.Element("Warnings").Value,
        RequestedCommand = r.Element("RequestedCommand").Value,
        CommandResponse = r.Element("CommandResponse").Value,
        Server = r.Element("Server").Value
    }
);

I've also tried this query with the same doc just to see if a simple example worked.

var test = doc.Descendants("RequestedCommand").First().Value;

But both return null. So where am I going wrong? I'll eventually need to get at the top level elements and the deeper elements within CommandResponse. Any help with that would also be appreciated.

UPDATE

As Jon's answer mentioned, it was mainly an issue with not using the namespace when referencing the various elements. Also used doc.Elements() rather then doc.Root. Descendants().

Here's an updated working version.

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 = r.Element(ns + "CommandResponse").Value,
        Server = r.Element(ns + "Server").Value
    }
);

解决方案

The problem is that you're not using the namespace when you're looking for elements, descendants etc:

XNamespace ns = "http://api.namecheap.com/xml.response";
var doc = XDocument.Load(url);
var response = doc.Root
                  .Descendants(ns + "ApiResponse")
                  .Select(r => new {
                              Errors = r.Element(ns + "Errors").Value,
                              ...
                          });

(Note that you never need where 1 == 1 in LINQ... I've changed this from query expression syntax as it wasn't giving you anything.)

The namespace is inherited from the <ApiResponse> element as the default namespace for all the other elements, as it's just xmlns=... rather than specifying an alias.

Also note that if you've shown us the whole XML document, then the above won't find any elements, as you're asking for ApiReponse elements below the root element, whereas it is the root element.

这篇关于停留在基本的LINQ to XML查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆