2011-08-06 3 views
1

XML에 LINQ를 사용하여 XML 파일을 읽으려는 중이므로 다른 로직에서 동일한 로직을 사용했기 때문에 제 논리는 괜찮은 것처럼 보입니다. 그러나 이번에는 "객체 참조가 객체의 인스턴스로 설정되지 않았습니다."라는 메시지가 나타납니다. foreach 루프가 발생하면 오류가 발생합니다. 어떤 도움이라도 대단히 감사 할 것입니다.XML에 LINQ를 사용하여 XML을 읽으십시오.

C#의 논리

StringBuilder s=new StringBuilder(); 
      try 
      { 
       XDocument loaded = XDocument.Load(@"C:\logs\samplebrava.xml"); 
       XName qualifiedName = XName.Get("Author", "http://www.infograph.com"); 
       var xmlQuery = from b in loaded.Descendants(qualifiedName)        
           select new 
           { 
            Title = b.Element("Title").Value, 
            Author = b.Element("Author").Attribute("name").Value, 
            Comment = b.Element("Comment").Value 
           }; 
       foreach (var item in xmlQuery)      
       s.AppendLine(item.Author + " - " + item.Title + " - " + item.Comment); 
      } 
      catch (Exception ex) { } 

XML 파일

<?xml version="1.0" encoding="UTF-8"?> 
<IGCMarkupDocument xmlns="http://www.infograph.com" majorversion="2" minorversion="6" revision="0"> 
    <CDLInfo v1="3" v2="0" v3="2" v4="11"> 
     <DriverInfo v1="1" v2="5" v3="2" v4="4">Emf2DL</DriverInfo> 
     <DriverFormatVersion>1</DriverFormatVersion> 
    </CDLInfo> 
    <PageList> 
     <Page index="0" left="0.0" bottom="0.0" right="42001.0" top="29701.0"> 
     <AuthorList> 
      <Author name="gdfsuez\aiimiadmin"> 
       <Changemark id="0" time="1311772228" guid="59B4A74788C31F4DA8D96AB4607499C0" color="255|0|0" hyperlink="" comment=""> 
        <PointList pointcount="6"> 
        <Point> 
         <x>5224.39</x> 
         <y>27153.1</y> 
        </Point> 
        <Point> 
         <x>2882.42</x> 
         <y>27153.1</y> 
        </Point> 
        <Point> 
         <x>2882.42</x> 
         <y>24785.4</y> 
        </Point> 
        <Point> 
         <x>4756</x> 
         <y>24785.4</y> 
        </Point> 
        <Point> 
         <x>5224.39</x> 
         <y>25259</y> 
        </Point> 
        <Point> 
         <x>4756</x> 
         <y>25259</y> 
        </Point> 
        </PointList> 
        <Title>MJ1</Title> 
        <Comment>Test</Comment> 
        <Viewstate Extents="false" ZoomWidth="true"> 
        <Page>0</Page> 
        <ScaleFactor>0.0388562</ScaleFactor> 
        <Rotation>0.0</Rotation> 
        <EyePoint> 
         <x>21000.5</x> 
         <y>16369.8</y> 
        </EyePoint> 
        <DeviceRect> 
         <top>0</top> 
         <left>0</left> 
         <bottom>1037</bottom> 
         <right>1633</right> 
        </DeviceRect> 
        <LayerTable LayerCount="0"/> 
        </Viewstate> 
       </Changemark> 
      </Author> 
     </AuthorList> 
     </Page> 
    </PageList> 
</IGCMarkupDocument> 

답변

0

문제는 네임 스페이스 사용과입니다. 이 도움이

b.Element(qualifiedName + "Title") 

희망 :

당신은 제대로 네임 스페이스 인스턴스를 생성했지만 요소를 참조하기 위해, 당신은 다음과 같은 패턴을 적용해야합니다.

편집 :이 게시물을 삭제합니다. 당신의 출처를 나쁘게 읽었습니다.

2

<Author> 요소를 자신의 하위 요소로 일치 시키려고하고 있으며 <Title><Comment> 요소는 <Author> 요소의 직접 하위 요소가 아닙니다.

또한 모든 요소 쿼리에서 XML 네임 스페이스를 지정해야합니다. 당신은 상용구 코드의 최소한의 양 그렇게하는 XNamespace 인스턴스를 사용할 수 있습니다

XNamespace igc = "http://www.infograph.com"; 
var xmlQuery = from author in loaded.Descendants(igc + "Author") 
       select new { 
        Title = author.Descendants(igc + "Title").First().Value, 
        Author = author.Attribute("name").Value, 
        Comment = author.Descendants(igc + "Comment").First().Value 
       }; 
+0

당신이있는 바로 프레드릭 ...... 고마워, 그것은 작동 –

관련 문제