using System;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using System.Collections;
namespace assignment2_merge
{
///
/// This cleans up assignment 2's XML file. Specifically, it removes entries that have
/// a value for item_form.
///
class Class1
{
public static XmlTextWriter xwriter;
public static Regex regularExpression;
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
// Establish the writer
xwriter = new XmlTextWriter("fb_inventory_assignment3.xml", null);
xwriter.Formatting = Formatting.Indented;
xwriter.WriteStartDocument();
xwriter.WriteStartElement("inventory");
// Load an input XML file and select flower descriptions
XmlDocument oldInventory = new XmlDocument();
oldInventory.Load("fb_inventory_assignment2.xml");
XmlNodeList oldInventoryList;
XmlElement oldInventoryRoot = oldInventory.DocumentElement;
// load the old supplier file list
XmlDocument firstShipment = new XmlDocument();
firstShipment.Load("shipment435.xml");
XmlNodeList newInventoryListAlpha;
XmlElement firstShipmentRoot = firstShipment.DocumentElement;
newInventoryListAlpha = firstShipmentRoot.SelectNodes("/Shipment/Supplier/Plant");
|
Below is the start of going through the assignment 2 inventory and looking running a regular expression search for " itemForm". Those plants that do NOT have this tag will be written to the new XML inventory file.
|
// BEGIN oldInventory "itemForm" search
// set the inventory list to deal values of itemForm
oldInventoryList = oldInventoryRoot.SelectNodes(
"/inventory/plant");
// Process target nodes
try
{
foreach (XmlNode d in oldInventoryList)
{
bool blnItemFormExists=false;
// Searches for ""
regularExpression = new Regex("",RegexOptions.IgnoreCase);
Match mItemForm = regularExpression.Match(d.OuterXml);
if (mItemForm.Success)
{
blnItemFormExists=true;
}
if (blnItemFormExists==false)
{
string itemId = d["itemID"].InnerText;
string itemName = d["itemName"].InnerText;
string itemDesc = d["itemDesc"].InnerText;
string itemType = d["itemType"].InnerText;
string itemSupplier ="blaa";
//deal with suppliers in the assignment2 data
foreach (XmlNode s in newInventoryListAlpha)
{
XmlAttributeCollection fIdAlpha;
fIdAlpha = s.Attributes;
if (itemId==fIdAlpha["ID"].InnerText)
{
XmlAttributeCollection fsupplierAttribAlpha;
fsupplierAttribAlpha = s.ParentNode.Attributes;
itemSupplier = fsupplierAttribAlpha["Name"].InnerText;
}
}
newInventoryWriterPlant(xwriter, itemType, itemId, itemName, itemDesc, itemSupplier);
//deal with multiple tags
regularExpression = new Regex("",RegexOptions.IgnoreCase);
Match mItemColor = regularExpression.Match(d.OuterXml);
if (mItemColor.Success)
{
xwriter.WriteStartElement("plantColors");
foreach(XmlElement xe in d)
{
if (xe.LocalName=="itemColor")
{
xwriter.WriteElementString("itemColor", xe.InnerText);
}
}
xwriter.WriteEndElement();
xwriter.WriteEndElement();
}
}
else
{
}
}
}
catch (System.NullReferenceException caught)
{
Console.WriteLine(caught.Message);
}
// END oldInventory itemForm search
|
Below is the start of going through new inventory. It finds color by calling the colorFinder method and running a regular expression search for various colors.
|
//BEGIN newInventory writer
// Get the shipment data
XmlDocument shipmentDoc = new XmlDocument();
shipmentDoc.Load("Shipment588.xml");
// Get the description data
XmlDocument verbenaDoc = new XmlDocument();
verbenaDoc.Load("VerbenaDescriptions.xml");
XmlDocument bellflowerDoc = new XmlDocument();
bellflowerDoc.Load("BellflowerDescriptions.xml");
XmlDocument dianthusDoc = new XmlDocument();
dianthusDoc.Load("DianthusDescriptions.xml");
// Collect target nodes
XmlNodeList newInventoryList;
XmlElement shipmentRoot = shipmentDoc.DocumentElement;
newInventoryList = shipmentRoot.SelectNodes(
"/Shipment/Supplier/Plant");
// Process target nodes
foreach (XmlNode n in newInventoryList)
{
string newInvName = n.ChildNodes[0].InnerText;
// Add the supplier name
XmlAttributeCollection fsupplierAttrib;
fsupplierAttrib = n.ParentNode.Attributes;
string newSupplierName = fsupplierAttrib["Name"].InnerText;
// Get the attribute data of node
XmlAttributeCollection nac;
nac = n.Attributes;
string newInvID = nac["ID"].InnerText;
string newInvType = nac["Type"].InnerText;
string newInvDesc = "no information available";
if (newInvType=="Verbena")
{
XmlElement verbenaRoot = verbenaDoc.DocumentElement;
XmlNodeList verbenaList = verbenaRoot.SelectNodes(
"/FlowerMania/Descriptions/ID");
foreach (XmlNode fdesc in verbenaList)
{
XmlAttributeCollection fdescAttrib;
fdescAttrib = fdesc.Attributes;
if (newInvID == fdescAttrib["Num"].InnerText)
{
newInvDesc = fdesc.ChildNodes[0].InnerText;
}
}
}
else if (newInvType=="Bellflower")
{
XmlElement bellflowerRoot = bellflowerDoc.DocumentElement;
XmlNodeList bellflowerList = bellflowerRoot.SelectNodes(
"/FlowerMania/Descriptions/ID");
foreach (XmlNode fdesc in bellflowerList)
{
XmlAttributeCollection fdescAttrib;
fdescAttrib = fdesc.Attributes;
if (newInvID == fdescAttrib["Num"].InnerText)
{
newInvDesc = fdesc.ChildNodes[0].InnerText;
}
}
}
else if (newInvType=="Dianthus")
{
XmlElement dianthusRoot = dianthusDoc.DocumentElement;
XmlNodeList dianthusList = dianthusRoot.SelectNodes(
"/FlowerMania/Descriptions/ID");
foreach (XmlNode fdesc in dianthusList)
{
XmlAttributeCollection fdescAttrib;
fdescAttrib = fdesc.Attributes;
if (newInvID == fdescAttrib["Num"].InnerText)
{
newInvDesc = fdesc.ChildNodes[0].InnerText;
}
}
}
else newInvDesc = "no description available";
flowerWriter(xwriter, newInvName, newInvID, newInvType, newInvDesc, newSupplierName);
}
//End new Inventory writer
// Close the writer
xwriter.WriteEndDocument();
xwriter.Close();
}
|
Below is a method that writes out the new inventory, not the existing inventory
|
public static void flowerWriter(XmlTextWriter wtx,
string inName, string inID, string inType, string inDesc, string inSupplier)
{
wtx.WriteStartElement("plant");
wtx.WriteElementString("itemType", inType);
wtx.WriteElementString("itemID", inID);
wtx.WriteElementString("itemName", inName);
wtx.WriteElementString("itemDesc", inDesc);
wtx.WriteElementString("itemSupplier", inSupplier);
wtx.WriteStartElement("plantColors");
ColorFinder(wtx, inDesc);
wtx.WriteEndElement();
wtx.WriteEndElement();
}
|
Below is a method that writes out the old inventory into the new format
|
public static void newInventoryWriterPlant(
XmlWriter xw, string invItemType, string invItemID,
string invItemName, string invItemDesc, string invItemSupplier)
//used to write inventory2 info into inventory3 format.
{
xw.WriteStartElement("plant");
xw.WriteElementString("itemType", invItemType);
xw.WriteElementString("itemID", invItemID);
xw.WriteElementString("itemName", invItemName);
xw.WriteElementString("itemDesc", invItemDesc);
xw.WriteElementString("itemSupplier", invItemSupplier);
}
|
This is the method that I use for dealing with color in new flower descriptions.
|
public static void ColorFinder(XmlWriter xw, string invItemDesc)
{
bool blnPink=false;
bool blnOrange=false;
bool blnRed=false;
bool blnYellow=false;
bool blnGreen=false;
bool blnPurple=false;
bool blnBlue=false;
// Create a regular expression to target the word "red"
regularExpression = new Regex("red|raspberry|scarlet|burgundy",RegexOptions.IgnoreCase);
// Searches for "red"
Match mRed = regularExpression.Match(invItemDesc);
// If "red" is found
if (mRed.Success)
{
if (blnRed==false)
{
xw.WriteElementString("itemColor", "red");
blnRed=true;
}
}
// Create a regular expression to target the word "yellow"
regularExpression = new Regex("yellow|gold",RegexOptions.IgnoreCase);
// Searches for "yellow"
Match mYellow = regularExpression.Match(invItemDesc);
// If "yellow" is found
if (mYellow.Success)
{
if (blnYellow==false)
{
xw.WriteElementString("itemColor", "yellow");
blnYellow=true;
}
}
// Create a regular expression to target the word "pink"
regularExpression = new Regex("pink",RegexOptions.IgnoreCase);
// Searches for "pink"
Match mPink = regularExpression.Match(invItemDesc);
// If "pink" is found
if (mPink.Success)
{
xw.WriteElementString("itemColor", "pink");
blnPink=true;
}
// Create a regular expression to target the word "white"
regularExpression = new Regex("white",RegexOptions.IgnoreCase);
// Searches for "white"
Match mWhite = regularExpression.Match(invItemDesc);
// If "white" is found
if (mWhite.Success)
{
xw.WriteElementString("itemColor", "white");
}
// Create a regular expression to target the word "orange"
regularExpression = new Regex("orange",RegexOptions.IgnoreCase);
// Searches for "orange"
Match mOrange = regularExpression.Match(invItemDesc);
// If "orange" is found
if (mOrange.Success)
{
xw.WriteElementString("itemColor", "orange");
blnOrange=true;
}
// Create a regular expression to target the words "peach" or "apricot"
regularExpression = new Regex("peach|apricot",RegexOptions.IgnoreCase);
Match mPeach = regularExpression.Match(invItemDesc);
// If "peach" is found
if (mPeach.Success)
{
if (blnPink==false)
{
xw.WriteElementString("itemColor", "pink");
blnPink=true;
}
if (blnOrange==false)
{
xw.WriteElementString("itemColor", "orange");
blnOrange=true;
}
}
// Create a regular expression to target the word "rose"
regularExpression = new Regex("rose",RegexOptions.IgnoreCase);
// Searches for "rose"
Match mRose = regularExpression.Match(invItemDesc);
// If "rose" is found
if (mRose.Success)
{
if (blnRed==false)
{
xw.WriteElementString("itemColor", "red");
blnRed=true;
}
if (blnPink==false)
{
xw.WriteElementString("itemColor", "pink");
blnPink=true;
}
}
// Create a regular expression to target the word "green"
regularExpression = new Regex("green",RegexOptions.IgnoreCase);
// Searches for "green"
Match mGreen = regularExpression.Match(invItemDesc);
if (mGreen.Success)
{
if (blnGreen==false)
{
xw.WriteElementString("itemColor", "green");
blnGreen=true;
}
}
// Create a regular expression to target the word "purple"
regularExpression = new Regex("purple|lilac|violet|lavender",RegexOptions.IgnoreCase);
// Searches for "purple"
Match mPurple = regularExpression.Match(invItemDesc);
if (mPurple.Success)
{
if (blnPurple==false)
{
xw.WriteElementString("itemColor", "purple");
blnPurple=true;
}
}
// Create a regular expression to target the word "blue"
regularExpression = new Regex("blue",RegexOptions.IgnoreCase);
// Searches for "blue"
Match mBlue = regularExpression.Match(invItemDesc);
if (mBlue.Success)
{
if (blnBlue==false)
{
xw.WriteElementString("itemColor", "blue");
blnBlue=true;
}
}
}
}
}
|