0

我有一个奇怪的问题,我尝试了有关 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>

4

1 回答 1

1

问题是查询的这一部分:

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

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

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

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

此外,我希望您想要each的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")
};

(目前尚不清楚为什么你要完全限定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。因此,如果您遇到问题,一定有一些您没有告诉我们的事情。

于 2012-10-24T15:52:16.703 回答