Click here to Skip to main content
15,747,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing code in C++ on Visual Studio 2015 and using standard XML libs that comes with Windows APIs.

Below is a sample XML file that i have.

       <name>Sample XXX</name>

The task i have is to find the line number for a particular tag. I did some research online and couldn't find a way to get the line numbers.

For e.g., i want to get the line number for tag documentType

I didn't have much luck doing this in C++. i.e., i couldn't find a library that will get me the line numbers. Currently am using MSXML lib.

Can someone guide me whether this is possible to do in C++? Any example code will be really helpful.

What I have tried:

Currently am incrementing an integer in the loop where it traverses through the nodes of the XML. And this integer will hold the line number for a particular tag. This is not a proper logic and will fail if there are line breaks in the XML file.
Updated 3-Dec-19 0:54am
[no name] 28-Jan-17 13:40pm    
There is no magic.
nv3 29-Jan-17 4:47am    
My best guess is that MSXML will not allow you to find the line number of a tag. You will probably need to find another XML library or implement your own parser that keeps track of line numbers.
11917640 Member 4-Dec-19 10:05am    
You can use Microsoft XmlLite SDK: IXMlReader::GetLineNumber is the feature you need: However, XmlLite is relatively low-level SDK, and parsing XML document is not easy. You cannot load the whole document at once, and need to build the parsing logic in the program.

Even an XML file is a text file, so you can search for the position of the tag and than count the CR codes til the position of your tag.

Here is some example code without MFC. Or use the Find function in a similiar way.
Share this answer
Donguy1976 29-Jan-17 17:06pm    
This solution will work if there's only one tag of the particular type. What if there're multiple tags of same type? i.e., if i want line number for tag <documenttype> and then it's present in multiple places in the XML file.
i couldn't find a library that will get me the line numbers.

The reason is that the line number is meaningless. the lines and indentation exist only to help the reading by humans.
Technically, an XML can be a single line. That is why nobody care about line numbers.

If you really want to use line numbers, I fear you will have to build your own solution.
Share this answer
Donguy1976 29-Jan-17 23:32pm    
I understand that XML can be single line. But in my case we're making sure that the XML is multiple lines and the purpose of getting line number is to let user know if there's an error in one or multiple tags. So are you saying that there's no way of doing it in C++? Other than writing my own XML parsing logic to get line numbers?
Patrice T 30-Jan-17 0:39am    
"Other than writing my own XML parsing logic to get line numbers?"
Yes, or you have to find a library that allow you to use line numbers.
I don't know one.
If you're using C++ on Visual Studio and need line numbers for a file with CRs at the end of each line, you can probably do what I do with C#. Read the XML file ("myFile") directly in to a String array "myText". Rather than obtaining line numbers from 1 - 22, you get an arrays of strings with zero-based index 0-21, each string being a line from your XML file. Thus "documentType" will be found in myText[10] which corresponds to line 11 of your XML:

String[] myText = File.ReadAllLines(myFile);

That's what I do with Visual Studio using C# developing Windows Forms apps.
Share this answer
Richard Deeming 3-Dec-19 9:35am    
With C# there's a much simpler option - load the XDocument with LoadOptions.SetLineInfo, cast the node to IXmlLineInfo, and read its LineNumber property:
c# - get line number for XElement here - Stack Overflow[^]

But obviously that's not going to help the OP, who's using C++. :)

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900