Created
August 21, 2009 21:07
-
-
Save nickmartini/172429 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Data; | |
using System.Net; | |
using System.IO; | |
using System.Xml; | |
using System.Xml.Linq; | |
namespace DONGS.PreshipParser | |
{ | |
public class PreshipParser | |
{ | |
private UriBuilder url = Helpers.ServiceLocation; | |
private FileInfo file; | |
private DataClassesContext context = new DataClassesContext(); | |
private Group group; | |
public PreshipParser(string accountNumber) | |
{ | |
if (!string.IsNullOrEmpty(this.url.Query)) | |
{ | |
this.url.Query += "&P_KUNNR=" + accountNumber; | |
} | |
else | |
{ | |
this.url.Query = "P_KUNNR=" + accountNumber; | |
} | |
this.group = this.context.Customers.Where(c => c.accountnumber == accountNumber).First().Group; | |
this.file = new FileInfo(string.Format(@"{0}\{1}-{2}-{3}.xml", Helpers.DownloadsDirectory.FullName, accountNumber, this.group.group_id, DateTime.Now.Ticks)); | |
//HACK: things that are wrong with their XML which in turn necessitates extra steps: | |
// - blank lines before the <?xml?> header | |
// -> just downloading the xml as a string, trimming it, and saving it to a file | |
// - not a standard case, and XML is hardcore case sensitive | |
// -> calling .ToUpper() before saving | |
// -> replacing the header with the proper format header | |
using (WebClient client = new WebClient()) | |
{ | |
string temp = client.DownloadString(url.ToString()) | |
.ToUpper() | |
.Trim() | |
.Replace("XML VERSION=", "xml version=") | |
.Replace("ENCODING=", "encoding="); //YEAH :D | |
using (StreamWriter d = this.file.AppendText()) | |
{ | |
d.Write(temp); | |
} | |
} //end crappy hack | |
XDocument data = XDocument.Load(this.file.FullName); | |
var q = from x in data.Elements("TRANSFER").Elements("MESSAGE") | |
select x.Value; | |
foreach (var d in q) | |
{ | |
Console.WriteLine(d); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment