Click here to Skip to main content
15,887,175 members
Articles / Programming Languages / XML

Getting LineNumber(s) in your XLINQ

Rate me:
Please Sign up or sign in to vote.
4.86/5 (9 votes)
26 Jun 2015CPOL 12.8K   3   2
Getting LineNumber(s) in your XLINQ

At work, the other day, I had to do some work with some XML fragments, which I decided to do using XLinq.

Where I wanted to validate a certain fragment, and also get line numbers out of the fragment when it was deemed invalid. Say I had this XML:

XML
<?xmlversion="1.0" encoding="utf-8"?>
<Clients>
  <Client>
    <FirstName>Travis</FirstName>
    <LastName>Bickle</LastName>
  </Client>
  <Client>
    <FirstName>Franics</FirstName>
    <LastName>Bacon</LastName>
  </Client>
</Clients>

XLine actually supoprts line numbers by the way of the IXmlLineInfo Interface.

So say, you had some code like this which grabbed a XNode and wanted to use its line number:

C#
XText travis = (from x in xml.DescendantNodes().OfType<XText>()
                where x.Value == "Travis"
                select x).Single();

var lineInfo = (IXmlLineInfo)travis;
Console.WriteLine("{0} appears on line {1}", travis, lineInfo.LineNumber);

What I was finding though was that my line numbers were always coming out with 0 reported as the lineNumber. Turns out there is a easy win for this, it is to do with how I was initially loading the XDocument. I was doing this:

C#
var xml = XDocument.Load(file);

Which is bad, and will not load the line numbers. You need to do this instead:

C#
var xml = XDocument.Load(file, LoadOptions.SetLineInfo);

For a much better write up on all of this, check out this old post by Charlie Calvert, it's much better than my post, wish I had found that one first:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
BugSecond code block Pin
Brisingr Aerowing26-Jun-15 11:42
professionalBrisingr Aerowing26-Jun-15 11:42 
GeneralRe: Second code block Pin
Sacha Barber28-Jun-15 21:34
Sacha Barber28-Jun-15 21:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.