开发者

Read xml using LINQ to Xml

im trying to read an xml file using LINQ to XML, my logic seems to be fine as i have used the same logic in different application. but this time i gets "Object reference not set to an instance of an object." error when i get to foreach loop. Any help will be much appreciate.

C# Logic

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 File

<?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>


You're trying to match the <Author> elements as children of themselves, and the <Title> and <Comment> elements are not direct children of <Author> elements.

You also have to specify the XML namespace in all your element queries. You can use an XNamespace instance to do that with a minimal amount of boilerplate code:

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


The problem is with the namespace usage.

You have created correctly a namespace instance, but to refer an element, you should apply the following pattern:

b.Element(qualifiedName + "Title")

Hope this helps.

EDIT: Discard this post. I read bad the your source.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜