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>
당신이있는 바로 프레드릭 ...... 고마워, 그것은 작동 –