I have tried to read XML file using Linq. Here is my code.
using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Text; using System.Threading.Tasks; namespace LinqToXml { class Program { static void Main(string[] args) { XDocument xdoc = XDocument.Load(@"E:\\XML\\test.xml"); xdoc.Descendants("Title").Select(t => new { fontType = t.Attribute("fontType").Value, fontSize = t.Attribute("fontSize").Value, fontColor = t.Attribute("fontColor").Value, Underline = t.Attribute("Underline").Value, Bold = t.Attribute("Bold").Value, Italic = t.Attribute("Italic").Value, }).ToList().ForEach(t => { Console.WriteLine("fontType : " + t.fontType); Console.WriteLine("fontSize : " + t.fontSize); Console.WriteLine("fontColor : " + t.fontColor); Console.WriteLine("Underline : " + t.Underline); Console.WriteLine("Bold : " + t.Bold); Console.WriteLine("Italic : " + t.Italic); }); Console.ReadLine(); } } } It gives an below exception
An unhandled exception of type 'System.NullReferenceException' occurred in programm.exe
But I can't find what is the error. Can anyone help me..
This is the XML file which I have used..
<Styles> <Title> <fontType>TimesNewRoman</fontType> <fontSize>20</fontSize> <fontColor>black</fontColor> <Underline>No</Underline> <Bold>bold</Bold> <Italic>No</Italic> </Title> <Title> <fontType>TimesNewRoman</fontType> <fontSize>20</fontSize> <fontColor>black</fontColor> <Underline>No</Underline> <Bold>bold</Bold> <Italic>italic</Italic> </Title> </Styles>
No comments:
Post a Comment