Skip to content

Instantly share code, notes, and snippets.

@kuzmik kuzmik/850.cs
Created Aug 8, 2017

Embed
What would you like to do?
XML EDI parser
public void ParseFile()
{
if (!this.hasdeliveries)
return;
XDocument data = XDocument.Load(this.file.FullName);
var q = from x in data.Elements("TRANSFER").Elements("MESSAGE")
select x.Value;
//We are checking to see if there is a message that says "information sent on XX deliveries" to validate the xml.
Match match = new Regex(@"INFORMATION SENT ON\s+(?<dels>\d+) DELIVERIES", RegexOptions.ExplicitCapture).Match(q.First());
if (!match.Success)
{
Helpers.MainLogger.Warn("XML file doesnt seem to be valid. {0}\n{1}", this.file.FullName, this.customer.URL);
Console.WriteLine("XML File doesn't seem to be valid.");
return;
}
Console.WriteLine("Deliveries for {0} / {1}: {2}", this.customer.Group.name, this.customer.accountnumber, match.Groups["dels"].Value);
//Deliveries
//<DELITEM>
// <CUST>0000101330</CUST>
// <DELIVERY>0612377970</DELIVERY>
// <DELDATE>20090618</DELDATE>
// <CUSTPO>OPAM061209MR</CUSTPO>
// <AUART>ZKB</AUART>
// <TRANSMETHOD>FED2</TRANSMETHOD>
//</DELITEM>
var dels = from d in data.Element("TRANSFER").Elements("DELIVERIES").Elements("DELITEM")
select new Delivery
{
delivery_id = Convert.ToInt32(d.Element("DELIVERY").Value),
account_number = Convert.ToInt32(d.Element("CUST").Value).ToString(),
custpo = d.Element("CUSTPO").Value,
order_method = d.Element("AUART").Value,
delivery_date = DateTime.Parse(d.Element("DELDATE").Value.Insert(4, "-").Insert(7, "-")),
};
foreach (var d in dels)
{
///Delivery line items
//<LINEITEM>
// <DELIVERY>0612380480</DELIVERY>
// <PROD>NXG1BU0170</PROD>
// <LSTYLE>NXG1BU</LSTYLE>
// <QUANTITY> 1.000</QUANTITY> NICE FORMATTING DUHHHHH
// <SERIAL>4511160703</SERIAL>
// <EXPDATE>20120301</EXPDATE>
//</LINEITEM>
var items = from i in data.Element("TRANSFER").Elements("LINES").Elements("LINEITEM")
where Convert.ToInt32(i.Element("DELIVERY").Value) == d.delivery_id
select new Item
{
matnbr = i.Element("PROD").Value,
quantity = (int)Convert.ToDecimal(i.Element("QUANTITY").Value.Trim()),
serial = i.Element("SERIAL").Value,
expiration = DateTime.Parse(i.Element("EXPDATE").Value.Insert(4, "-").Insert(7, "-"))
};
d.Items.AddRange(items);
//Tracking numbers
//<TRACKITEM>
// <DELIVERY>0612400405</DELIVERY>
// <BOLNR>418461350796</BOLNR>
//</TRACKITEM>
var tracking = from t in data.Element("TRANSFER").Elements("TRACKING").Elements("TRACKITEM")
where Convert.ToInt32(t.Element("DELIVERY").Value) == d.delivery_id
select new TrackingNumber
{
tracking_number = t.Element("BOLNR").Value
};
d.TrackingNumbers.AddRange(tracking);
this.customer.Group.Deliveries.Add(d);
}
try
{
this.context.SubmitChanges();
}
catch (Exception ex)
{
Helpers.MainLogger.ErrorException("Caught exception in Parser.ParseFile()", ex);
Console.WriteLine("Caught exception in Parser.ParseFile()", ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.