我有一個奇怪的問題,我嘗試了一切有關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>
你還沒有說過是什麼問題。你已經給出了一些代碼和XML文檔的*部分*,但實際上並沒有提出一個問題。 –
對不起,我添加了該問題。 –