2012-10-24 7 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") 
}; 

(당신이 System.Xml.Linq.XNamespace 완전히 자격 요건을 갖춘있어 왜이 방법으로 분명하지 않다, 또는 당신이 당신의 변수에 대한 l 접두사를 가지고 왜 ... 당신이 만들 수있는 많은 것을 할 수 당신이 만든 때 명확한 코드, 그것은 작동)

편집 :. 당신이 작동하지 않습니다 말했다 코드는 나를 위해 잘 작동합니다 : 3. 그래서 당신이 발생하는 경우 문제가 인쇄

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()); 
    } 
} 

그것으로, 당신이 우리에게 말하지 않은 어떤 것이 있어야합니다.

+0

답장을 보내 주셔서 감사합니다. ".ToString()"부분을 이해했습니다. 또한 "lName"대신 "lANamespace"를 사용해야한다는 사실을 알고 있습니다. 하지만 Qucik View에서이 부분을 확인하고 있었는데 아무 것도 반환하지 않았습니다. -> "xDoc.Element (lName +"GetCategoriesResponse ") 요소 (lName +"GetCategoriesResult ") 요소 (lANamespace +"CategoryClass ")". 이 성명서의 내용은 무엇입니까? –

+0

그리고 조언 해 주셔서 감사합니다. 나는 다음 번에 코드를 더 명확하게 할 것이다. 나는 실제로 내가 약간 부주의 한 이유 인 프로토 타입을 연구 중이다. –

+0

@Behroz : 많은 사람들에게 코드를 살펴달라고 요청하는 경우 더 많은주의를 기울일 필요가 있습니다. 나는 다른 모습을 가지고 코드를 직접 사용해 보겠습니다. –

관련 문제