2012-10-24 113 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:如果你要問很多其他人看你的代碼,這是值得考慮的關懷。我會再看一遍,並自己嘗試一下代碼。 –