I have this xml file and I want to extract author name and access number and what I have is a very naive implementation in C# where I am using xml reader and reading line by line. But I am looking for an implementation where I can read the author name and access number in c# efficiently. I am new to C# and I have been told that LINQ should be used but looking at the document and this file I am not able to relate how to use Xdocument. Any help will be appreciated.
<xml>
<records>
<record>
<database name="CP_EndnoteLibrary_2012-2015-1.enl" path="C:\Users\Downloads\file.enl">file.enl</database>
<source-app name="EndNote" version="17.4">EndNote</source-app>
<rec-number>24</rec-number>
<contributors>
<authors>
<author>
<style face="normal" font="default" size="100%">ABCD, X.</style>
</author>
<author>
<style face="normal" font="default" size="100%">EFGH, I.</style>
</author>
</authors>
</contributors>
<accession-num>
<style face="normal" font="default" size="100%">12345678</style>
</accession-num>
</record>
<record>...</record>
</records>
Following a document, I was able to write this code to figure out author name.
{
class Program
{
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create("C:\\Users\\ile_xml.xml");
while(reader.Read())
{
if((reader.NodeType == XmlNodeType.Element) && (reader.Name == "author"))
{
reader.Read();
reader.Read();
if((reader.NodeType == XmlNodeType.Element) && (reader.Name == "style") && reader.HasAttributes)
{
var val = reader.ReadInnerXml();
Console.WriteLine("Display:" + reader.GetAttribute("author"));
}
}
}
}
}
}
The above code seems to be very inefficient and I am looking for ways to improve this or do it in a better way.