2012-10-24 201 views
0

我有一个奇怪的问题,我尝试了一切有关XDocument。XDocument多个命名空间

我想获得“CategoryClass”节点的值,并填充我自己的“ListBoxClass”类型对象中的子节点。但是LINQ查询没有返回任何东西。

System.Xml.Linq.XNamespace lName = xDoc.Root.GetDefaultNamespace(); 
     System.Xml.Linq.XNamespace lANamespace = "http://schemas.datacontract.org/2004/07/KRefreshService"; 

     var lEle = from xml2 in xDoc.Element(lName + "GetCategoriesResponse").Element(lName + "GetCategoriesResult").Elements(lANamespace + "CategoryClass") 
                select new ListBoxClass 
                { 
                 Id = (int)xml2.Element(lName + "ID"), 
                 Name = xml2.Element(lName+ "CatName").ToString(), 
                 Description = xml2.Element(lName + "CatDescription").ToString() 
                }; 

这里是XML

<GetCategoriesResponse xmlns="http://tempuri.org/"> 
<GetCategoriesResult xmlns:a="http://schemas.datacontract.org/2004/07/KRefreshService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<a:CategoryClass> 
    <a:CatDescription>General questions regarding IQ. These questions will help you prepare for the interviews.</a:CatDescription> 
    <a:CatName>IQ</a:CatName> 
    <a:ID>1</a:ID> 
</a:CategoryClass> 
<a:CategoryClass> 
    <a:CatDescription>This category will help you improve your general knowledge. It have information from all the different subjects.</a:CatDescription> 
    <a:CatName>General Knowledge</a:CatName> 
    <a:ID>2</a:ID> 
</a:CategoryClass> 
<a:CategoryClass> 
    <a:CatDescription>If you feel that you computer science basics are slipping by your head as you involve more in technology. No problem! subscribe to this category.</a:CatDescription> 
    <a:CatName>Computer Science</a:CatName> 
    <a:ID>3</a:ID> 
</a:CategoryClass> 

+0

你还没有说过是什么问题。你已经给出了一些代码和XML文档的*部分*,但实际上并没有提出一个问题。 –

+0

对不起,我添加了该问题。 –

回答

1

的问题是查询的这个部分:

你在这里使用了错误的命名空间 - 这些元素有a前缀:

<a:CatDescription>...</a:CatDescription> 
<a:CatName>IQ</a:CatName> 
<a:ID>1</a:ID> 

...所以他们使用命名空间http://schemas.datacontract.org/2004/07/KRefreshService"。您正在使用在文档的根元素中声明的名称空间。

而且我希望你希望每个XElement,我倾向于用显式转换来获取到string。调用ToString()会给你该元素的缩进的XML。所以,你想:

select new ListBoxClass 
{ 
    Id = (int) xml2.Element(lANamespace + "ID"), 
    Name = (string) xml2.Element(lANamespace + "CatName"), 
    Description = (string) xml2.Element(lANamespace + "CatDescription") 
}; 

(为什么你完全qualifiying System.Xml.Linq.XNamespace目前尚不清楚,顺便说一下,为什么你已经为自己的变量的l前缀...你可以做很多事情,使这个代码更清晰,当你做了它的工作)

编辑:你说不行代码的行为对我罚款:

using System; 
using System.Linq; 
using System.Xml.Linq; 

public class Test 
{ 
    static void Main(string[] args) 
    { 
     XDocument doc = XDocument.Load("test.xml"); 
     XNamespace envelopeNs = doc.Root.GetDefaultNamespace(); 
     XNamespace resultNs = "http://schemas.datacontract.org/2004/07/KRefreshService"; 

     Console.WriteLine(doc.Element(envelopeNs + "GetCategoriesResponse") 
          .Element(envelopeNs + "GetCategoriesResult") 
          .Elements(resultNs + "CategoryClass") 
           .Count()); 
    } 
} 

,打印3,因此,如果您遇到问题有了它,一定有一些你没有告诉过我们的东西。

+0

谢谢主席先生的回复。我了解“.ToString()”部分。此外,我是知道的事实,我需要使用“lANamespace”而不是“L-NAME”。但是我在Qucik View中查看了这部分内容,并没有返回任何内容。 - >“xDoc.Element(lName +”GetCategoriesResponse“)。元素(lName +”GetCategoriesResult“)。元素(lANamespace +”CategoryClass“)”。这个声明有什么错误? –

+0

并感谢您的建议。下一次我会让代码更清晰。我实际上正在研究一个原型,这就是为什么我有点粗心。 –

+0

@Behroz:如果你要问很多其他人看你的代码,这是值得考虑的关怀。我会再看一遍,并自己尝试一下代码。 –