2012-04-13 144 views
0

我有以下XMLLINQ与命名空间前缀xml的

<Location xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<Latitude>-1</Latitude> 
<Longtitude>-1</Longtitude> 
</Location> 

而且没有命名空间(XMLNS:我......),我可以使用查询

//xdoc is an XDocument loaded with the above xml 
var locCollection = from p in xdoc.Descendants("Location") 

命名空间下但没有前缀“我”,我可以使用查询

XNamespace ns = @"http://www.w3.org/2001/XMLSchema-instance" 
var locCollection = from p in xdoc.Descendants(ns + "Location") 

那么,如何应对TE“我”字头以下?

谢谢。

回答

0

问题是,当您提供名称空间前缀时,您需要明确地为该名称空间中的XML元素加上前缀。因此,在您的示例中,如果您明确指定了i:Location中的前缀,那么您的查询将起作用。

<i:Location xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <Latitude>-1</Latitude> 
    <Longtitude>-1</Longtitude> 
</i:Location> 

它的工作,而没有前缀的原因是因为出来的前缀指定的命名空间被认为是默认的命名空间,因此Location是默认作用域的命名空间。

+0

谢谢克里斯。这实际上起作用。但你看到的XML是我从WCF服务获得的回应。我使用了[DataContract(Namespace =“”)] public class Location {},它默认没有指定前缀(i:location)。我知道这是一个完全不同的问题,但是你碰巧知道解决方案吗? – user302581 2012-04-14 04:27:59

+0

您将需要提供更多的上下文,也许如果您发布另一个问题与再现问题的示例。 – 2012-04-14 08:27:27