2009-12-08 5 views
0

나는LINQ는

"<root>" 
    "<page>" 
    "<title>text</title>" 
    "<text attrib1="1">some text</text>" 
    "<text attrib1="2">some text</text>" 
    "<text attrib1="3">some text</text>" 
    "<text attrib1="4">some text</text>" 

    "</page>" 

    "<page>" 
    "<title>text</title>" 
    "<text attrib1="1">some text</text>" 
    "<text attrib1="2">some text</text>" 
    "<text attrib1="3">some text</text>" 
    "<text attrib1="4">some text</text>" 

    "</page>" 
    "</root>" 

지금 내가이

 "<root>" 
    "<title>text</title>" 
    "<text attrib1="4">some text</text>" 
    "<title>text</title>" 
    "<text attrib1="4">some text</text>" 
    "</root>" 

같은 결과 XML이 될 수 싶다 ""무시 다음과 같은 형식의 XML을 하나의 쿼리에서 달성 되었습니까? 나는 내가 행복하지 않다 두 개의 쿼리

 var titleElms = 
      from t in allElements 
      select 
       new 
       { 
        Title = t.Element("title") 
       }; 

     var textElms = 
      from t in allText 
      where (string)t.Attribute("attrib1").Value == "4" 
      select 
      t; 

를 사용하여 다음을 시도했다. 그래서 다른 방법이 있습니까? pls 도움이됩니다.

+0

당신이 XML이 아닌 텍스트로 무엇을하려고 보여줄 수있는 몇 가지 새로운 아이디어를 준다? – UpTheCreek

+0

안녕하세요 죄송합니다. 여기에 XML 태그를 붙여 넣을 수 없기 때문에 모든 태그가 제거되었습니다. ': – Amit

+0

C와 같이 코드 블록에 넣으십시오. – UpTheCreek

답변

2

나는이 쿼리가 Ienumerable 개체가 될 것이므로 정확히 원하는 것을 제공하지 않을 것이라고 생각합니다. 제목과 텍스트 필드가 많은 단일 개체가 필요한 곳을 의미합니다. 이것은 제목과 텍스트가 Ienumerable 객체 내의 객체 인 객체를 반환합니다.

그래서 예제에서는 두 개의 개체가있는 개체를 갖게됩니다.이 개체는 이제 각각 제목과 텍스트를 갖고 있습니다. 일단 이것이 있으면, 원하는 방식으로 XML을 빌드 할 수 있습니다. 그것은 정말 당신의 솔루션에서 다르지 않지만 요청한대로, 당신에게 일할 수있는 하나의 linq 쿼리를 제공합니다. 아니면 최소한 아이디어를 얻으세요.

XElement Results = XElement.Load("c:\\test.xml"); //load you xml 
    XNamespace NameSpace = Results.Name.Namespace; 
    var xe = (from page in Results.Elements(NameSpace + "page") 
       let title = page.Elements(NameSpace+"title")      
       let text = page.Elements(NameSpace+"text").Where(a=>a.Attribute("attrib1").Value.Equals("4")) 
       where text.Count() > 0 
     //the where is simply to remove any unncessary data 
     //if there was ever a page that didn't have attrib1 = 4 

      select new {title, text}); 

희망이, 적어도, 당신

+0

감사합니다 Kamal ... :) – Amit