Parsing XML file from the REST API
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
Please suggest how I can improve my code (like memory leakage, which pattern to use etc.). I have to parse a book XML in two ways: first from an FTP file and second from the REST API. I implemented the methods in one class.
public void GetBooksFromApi(int bookId, int bookNumber = 0)
string ApiBookIDPrefix = ConfigurationManager.AppSettings["ApiBookIDPrefix"].ToString();
string key = ConfigurationManager.AppSettings["BookApiKey"].ToString();
string apiBookIdValue = $"ApiBookIDPrefixbookId";
string restApiBookUrl = $"ApiDownloadUrl?BookID=apiBookIdValue&key=key&bookNumber=bookNumber&limit=1";
string bookName = String.Empty;
string clientFolderPath = String.Empty;
string bookContent = String.Empty;
try
//here to get response, parse xml and creating files process
using (Stream xmlResponseStream = GetRestApiResponse(restApiBookUrl))
IEnumerable<XElement> xmlRecords = ParseBookXml(xmlResponseStream);
if (xmlRecords == null)
throw new NullReferenceException($"No Book found to parse from rest api xml");
foreach (var xmlRecord in xmlRecords)
if (xmlRecord.Element("BookError") == null)
bookName = GetBookName(xmlRecord, bookId);
clientFolderPath = $@"BaseFolderbookId";
bookContent = xmlRecord.ToString();
if (!string.IsNullOrEmpty(bookContent))
CreateFile(clientFolderPath, bookName, bookContent);
catch (Exception ex)
log.Error($"Unable to retrieve Book for bookId.ToString() with URL", ex);
throw ex;
public IEnumerable<XElement> ParseBookXml(Stream response)
IEnumerable<XElement> bookRecords = null;
try
XDocument bookDoc = new XDocument();
bookDoc = XDocument.Load(response);
bookRecords = bookDoc.Root.Elements("bookReport").ToList();
return bookRecords;
catch (Exception ex)
log.Error($"Unable to load XML response from Rest Api Url .", ex);
return bookRecords;
public string GetBookName(XElement bookXmlRecord, int bookId)
string bookName = string.Empty;
string bookNumberString = String.Empty;
string bookNumberVal = bookXmlRecord.Element("bookNumber")?.Value;
DateTime bookDateVal = now(); // today date
bool validBookDate = DateTime.TryParse(bookXmlRecord.Element("BookDate")?.Value, out bookDateVal);
if (!validBookDate)
throw new InvalidCastException($"Unable to parse Book Date from rest api xml");
bookNumberString = GetBookNumberString(int.Parse(bookNumberVal));
bookName = $"Book-bookId.ToString().PadLeft(4, '0')-reportDate:yyyyMMddbookNumberString.xml";
return bookName;
public bool CreateFile(string clientFolder, string fileName, string xmlResponse)
try
string filePath = $@"clientFolderfileName";
if (!Directory.Exists(clientFolder))
Directory.CreateDirectory(clientFolder);
File.WriteAllText(filePath, xmlResponse);
return true;
catch (Exception ex)
log.Error($"Unable to create book in path clientFolder.", ex);
return false;
c# parsing .net xml rest
add a comment |Â
up vote
2
down vote
favorite
Please suggest how I can improve my code (like memory leakage, which pattern to use etc.). I have to parse a book XML in two ways: first from an FTP file and second from the REST API. I implemented the methods in one class.
public void GetBooksFromApi(int bookId, int bookNumber = 0)
string ApiBookIDPrefix = ConfigurationManager.AppSettings["ApiBookIDPrefix"].ToString();
string key = ConfigurationManager.AppSettings["BookApiKey"].ToString();
string apiBookIdValue = $"ApiBookIDPrefixbookId";
string restApiBookUrl = $"ApiDownloadUrl?BookID=apiBookIdValue&key=key&bookNumber=bookNumber&limit=1";
string bookName = String.Empty;
string clientFolderPath = String.Empty;
string bookContent = String.Empty;
try
//here to get response, parse xml and creating files process
using (Stream xmlResponseStream = GetRestApiResponse(restApiBookUrl))
IEnumerable<XElement> xmlRecords = ParseBookXml(xmlResponseStream);
if (xmlRecords == null)
throw new NullReferenceException($"No Book found to parse from rest api xml");
foreach (var xmlRecord in xmlRecords)
if (xmlRecord.Element("BookError") == null)
bookName = GetBookName(xmlRecord, bookId);
clientFolderPath = $@"BaseFolderbookId";
bookContent = xmlRecord.ToString();
if (!string.IsNullOrEmpty(bookContent))
CreateFile(clientFolderPath, bookName, bookContent);
catch (Exception ex)
log.Error($"Unable to retrieve Book for bookId.ToString() with URL", ex);
throw ex;
public IEnumerable<XElement> ParseBookXml(Stream response)
IEnumerable<XElement> bookRecords = null;
try
XDocument bookDoc = new XDocument();
bookDoc = XDocument.Load(response);
bookRecords = bookDoc.Root.Elements("bookReport").ToList();
return bookRecords;
catch (Exception ex)
log.Error($"Unable to load XML response from Rest Api Url .", ex);
return bookRecords;
public string GetBookName(XElement bookXmlRecord, int bookId)
string bookName = string.Empty;
string bookNumberString = String.Empty;
string bookNumberVal = bookXmlRecord.Element("bookNumber")?.Value;
DateTime bookDateVal = now(); // today date
bool validBookDate = DateTime.TryParse(bookXmlRecord.Element("BookDate")?.Value, out bookDateVal);
if (!validBookDate)
throw new InvalidCastException($"Unable to parse Book Date from rest api xml");
bookNumberString = GetBookNumberString(int.Parse(bookNumberVal));
bookName = $"Book-bookId.ToString().PadLeft(4, '0')-reportDate:yyyyMMddbookNumberString.xml";
return bookName;
public bool CreateFile(string clientFolder, string fileName, string xmlResponse)
try
string filePath = $@"clientFolderfileName";
if (!Directory.Exists(clientFolder))
Directory.CreateDirectory(clientFolder);
File.WriteAllText(filePath, xmlResponse);
return true;
catch (Exception ex)
log.Error($"Unable to create book in path clientFolder.", ex);
return false;
c# parsing .net xml rest
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Please suggest how I can improve my code (like memory leakage, which pattern to use etc.). I have to parse a book XML in two ways: first from an FTP file and second from the REST API. I implemented the methods in one class.
public void GetBooksFromApi(int bookId, int bookNumber = 0)
string ApiBookIDPrefix = ConfigurationManager.AppSettings["ApiBookIDPrefix"].ToString();
string key = ConfigurationManager.AppSettings["BookApiKey"].ToString();
string apiBookIdValue = $"ApiBookIDPrefixbookId";
string restApiBookUrl = $"ApiDownloadUrl?BookID=apiBookIdValue&key=key&bookNumber=bookNumber&limit=1";
string bookName = String.Empty;
string clientFolderPath = String.Empty;
string bookContent = String.Empty;
try
//here to get response, parse xml and creating files process
using (Stream xmlResponseStream = GetRestApiResponse(restApiBookUrl))
IEnumerable<XElement> xmlRecords = ParseBookXml(xmlResponseStream);
if (xmlRecords == null)
throw new NullReferenceException($"No Book found to parse from rest api xml");
foreach (var xmlRecord in xmlRecords)
if (xmlRecord.Element("BookError") == null)
bookName = GetBookName(xmlRecord, bookId);
clientFolderPath = $@"BaseFolderbookId";
bookContent = xmlRecord.ToString();
if (!string.IsNullOrEmpty(bookContent))
CreateFile(clientFolderPath, bookName, bookContent);
catch (Exception ex)
log.Error($"Unable to retrieve Book for bookId.ToString() with URL", ex);
throw ex;
public IEnumerable<XElement> ParseBookXml(Stream response)
IEnumerable<XElement> bookRecords = null;
try
XDocument bookDoc = new XDocument();
bookDoc = XDocument.Load(response);
bookRecords = bookDoc.Root.Elements("bookReport").ToList();
return bookRecords;
catch (Exception ex)
log.Error($"Unable to load XML response from Rest Api Url .", ex);
return bookRecords;
public string GetBookName(XElement bookXmlRecord, int bookId)
string bookName = string.Empty;
string bookNumberString = String.Empty;
string bookNumberVal = bookXmlRecord.Element("bookNumber")?.Value;
DateTime bookDateVal = now(); // today date
bool validBookDate = DateTime.TryParse(bookXmlRecord.Element("BookDate")?.Value, out bookDateVal);
if (!validBookDate)
throw new InvalidCastException($"Unable to parse Book Date from rest api xml");
bookNumberString = GetBookNumberString(int.Parse(bookNumberVal));
bookName = $"Book-bookId.ToString().PadLeft(4, '0')-reportDate:yyyyMMddbookNumberString.xml";
return bookName;
public bool CreateFile(string clientFolder, string fileName, string xmlResponse)
try
string filePath = $@"clientFolderfileName";
if (!Directory.Exists(clientFolder))
Directory.CreateDirectory(clientFolder);
File.WriteAllText(filePath, xmlResponse);
return true;
catch (Exception ex)
log.Error($"Unable to create book in path clientFolder.", ex);
return false;
c# parsing .net xml rest
Please suggest how I can improve my code (like memory leakage, which pattern to use etc.). I have to parse a book XML in two ways: first from an FTP file and second from the REST API. I implemented the methods in one class.
public void GetBooksFromApi(int bookId, int bookNumber = 0)
string ApiBookIDPrefix = ConfigurationManager.AppSettings["ApiBookIDPrefix"].ToString();
string key = ConfigurationManager.AppSettings["BookApiKey"].ToString();
string apiBookIdValue = $"ApiBookIDPrefixbookId";
string restApiBookUrl = $"ApiDownloadUrl?BookID=apiBookIdValue&key=key&bookNumber=bookNumber&limit=1";
string bookName = String.Empty;
string clientFolderPath = String.Empty;
string bookContent = String.Empty;
try
//here to get response, parse xml and creating files process
using (Stream xmlResponseStream = GetRestApiResponse(restApiBookUrl))
IEnumerable<XElement> xmlRecords = ParseBookXml(xmlResponseStream);
if (xmlRecords == null)
throw new NullReferenceException($"No Book found to parse from rest api xml");
foreach (var xmlRecord in xmlRecords)
if (xmlRecord.Element("BookError") == null)
bookName = GetBookName(xmlRecord, bookId);
clientFolderPath = $@"BaseFolderbookId";
bookContent = xmlRecord.ToString();
if (!string.IsNullOrEmpty(bookContent))
CreateFile(clientFolderPath, bookName, bookContent);
catch (Exception ex)
log.Error($"Unable to retrieve Book for bookId.ToString() with URL", ex);
throw ex;
public IEnumerable<XElement> ParseBookXml(Stream response)
IEnumerable<XElement> bookRecords = null;
try
XDocument bookDoc = new XDocument();
bookDoc = XDocument.Load(response);
bookRecords = bookDoc.Root.Elements("bookReport").ToList();
return bookRecords;
catch (Exception ex)
log.Error($"Unable to load XML response from Rest Api Url .", ex);
return bookRecords;
public string GetBookName(XElement bookXmlRecord, int bookId)
string bookName = string.Empty;
string bookNumberString = String.Empty;
string bookNumberVal = bookXmlRecord.Element("bookNumber")?.Value;
DateTime bookDateVal = now(); // today date
bool validBookDate = DateTime.TryParse(bookXmlRecord.Element("BookDate")?.Value, out bookDateVal);
if (!validBookDate)
throw new InvalidCastException($"Unable to parse Book Date from rest api xml");
bookNumberString = GetBookNumberString(int.Parse(bookNumberVal));
bookName = $"Book-bookId.ToString().PadLeft(4, '0')-reportDate:yyyyMMddbookNumberString.xml";
return bookName;
public bool CreateFile(string clientFolder, string fileName, string xmlResponse)
try
string filePath = $@"clientFolderfileName";
if (!Directory.Exists(clientFolder))
Directory.CreateDirectory(clientFolder);
File.WriteAllText(filePath, xmlResponse);
return true;
catch (Exception ex)
log.Error($"Unable to create book in path clientFolder.", ex);
return false;
c# parsing .net xml rest
edited Jun 4 at 6:34
t3chb0t
31.9k54195
31.9k54195
asked Jun 4 at 1:20
user171148
111
111
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195782%2fparsing-xml-file-from-the-rest-api%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password