Click here to Skip to main content
15,884,625 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I'm creating a BizTalk solution to process one purchase order.
customer,orderno,orddate,prodcode,proddesc,proddesc2,qty,price,deliverydt,delivery1
"700 KIDMAN WAY",66109,"03/03/15","CTV834","CTV834 GOSSIPS WHITE Cask Dry","4x5L 29313748009606",1200.0000,0.0000,"16/03/15","700 KIDMAN WAY"
"700 KIDMAN WAY",66109,"03/03/15","","**** run 18083 ****","",0.0000,0.0000,"03/03/15","700 KIDMAN WAY"
"700 KIDMAN WAY",66109,"03/03/15","CTV835","CTV835 GOSSIPS RED Cask Dry","4x5L 29313748009590",1200.0000,0.0000,"16/03/15","700 KIDMAN WAY"
"700 KIDMAN WAY",66109,"03/03/15","","**** run 18084 ****","",0.0000,0.0000,"03/03/15","700 KIDMAN WAY"

I want only alternate lines(1, 3.. etc) to get mapped and create XML file.

second line (4th line and so on) is invalid since there is no product code available.

So, the output XML file should contain only those lines having product code, in the same time line number should be proper.

Can we do it using BizTalk map or do we need to use csharp code?

Please help.

Thanks

Haris
Posted
Updated 1-Apr-15 18:51pm
v2

1 solution

You can use a regular expression like this to check if a row matches your criteria or not.
C#
Regex LineExpression  = new Regex("\"(?<customer>[\\S ]*?)\",(?<orderno>\\d*),\"(?<orddate>[\\S ]*?)\",\"(?<prodcode>[\\S ]+?)\",\"(?<proddesc>[\\S ]*?)\",\"(?<proddesc2>[\\S ]*?)\",(?<qty>\\d+\\.\\d*),(?<price>\\d+\\.\\d*),\"(?<deliverydt>[\\S ]*?)\",\"(?<delivery1>[\\S ]*?)\"");

http://www.regular-expressions.info/[^]

Notice that for the product code, the expression uses a '+' instead of a '*' as a quantifier.
This tells the regex engine that there should be at least on character inside the double quotes. Hence, lines without a product code will not match.

Then you can read either read the file line by line and check if it is match or read the whole file and loop through all matches.

C# example 1
C#
foreach (string line in File.ReadAllLines("filename.txt"))
{
    Match m = LineExpression.Match(line);
    if (m.Success)
    {
        string customer = m.Groups["customer"].Value;
        int orderNumber = int.Parse(m.Groups["orderno"].Value);
        DateTime orderDate = DateTime.ParseExact(m.Groups["orddate"].Value, "MM/dd/yy", null);
        // etc. etc.
    }
}


C# example 2
C#
string lines = File.ReadAllText("filename.txt");


foreach (Match m in LineExpression.Matches(lines))
{
    string customer = m.Groups["customer"].Value;
    int orderNumber = int.Parse(m.Groups["orderno"].Value);
    DateTime orderDate = DateTime.ParseExact(m.Groups["orddate"].Value, "MM/dd/yy", null);
    // etc. etc.
}
 
Share this answer
 
v3

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