Split XML file in C# - c#

I have an XML file which have multiple messages in one large file, my objective it to split the file into singe xml file for each message, I have a c# code which only gets me the first instance of the message. can you please tell what am I missing here:
Here is my code :
string strSeq;
string strFileName;
XDocument doc = XDocument.Load(#"C:\XMl\MR.xml");
var newDocs = doc.Descendants("Message")
.Select(d => new XDocument(new XElement("FileDump", d)));
foreach (var newDoc in newDocs)
{
strSeq = XDocument.Load(#"C:\XMl\MR.xml").XPathSelectElement
"//FileDump/Message/MsgID").Value;
strFileName = "MR_" + strSeq + ".xml";
newDoc.Save(Console.Out); Console.WriteLine();
newDoc.Save(#"C:\xml\MR\Tst\" + strFileName);
Console.WriteLine();
}

You should search for message ID within newDoc instead of doc:
foreach (var newDoc in newDocs)
{
strSeq = newDoc.XPathSelectElement("//FileDump/Message/MsgID").Value;
strFileName = "MR_" + strSeq + ".xml";
newDoc.Save(Console.Out); Console.WriteLine();
newDoc.Save(#"C:\xml\MR\Tst\" + strFileName);
Console.WriteLine();
}

Try,
string path = #"C:\xml\MR\Tst\MR_";
XElement root = XElement.Load(file);
foreach(XElement message in root.Descendants("Message"))
{
string id = message.Element("MsgID").Value;
message.Save(path + id + ".xml");
}

Related

convert xml file into flat file in a tabular format

I have many XMLs that I want to flatten out and save as a file (as below)using C#. One option that I tried is to use excel to import the file and then call the vba script from c#. Is there any option to do this in C#.
Sample input xml:
<request>
<log-date>11/28/2016 04:48:40</log-date>
<service-name>getPdf</service-name>
<request-id>1234</request-id>
<request-xml>
<MyRequest xmlns="http://abcd.com">
<GroupID>123</GroupID>
<ClientName>ACBD</ClientName>
<BrokerInfo>
<BrokerLoginName>9876</BrokerLoginName>
<FullName>John</FullName>
</BrokerInfo>
<BrokerInfo>
<BrokerLoginName>0987</BrokerLoginName>
<FullName>Mike</FullName>
</BrokerInfo>
</MyRequestRequest>
</request-xml>
</request>
Expected output file:
log-date|service-name|request-id|groupID|ClientName|BrokerLoginName|FullName
11/28/2016 04:48:40|getPdf|1234|123|ACBD|9876|John
11/28/2016 04:48:40|getPdf|1234|123|ACBD|0987|Mike
Here is your solution:
using System.IO;
using System.Xml;
public static void ReadInnerText()
{
StreamWriter file = new StreamWriter("myTextFile.txt");
file.WriteLine("log-date|service-name|request-id|groupID|ClientName|BrokerLoginName|FullName");
string Line = string.Empty;
string BrokerLoginName = string.Empty;
XmlDocument doc = new XmlDocument();
XmlNodeList SecondTag;
XmlNodeList ThirdTag;
XmlNodeList FourthTag;
XmlNodeList FifthTag;
doc.Load("inputFile.xml");
XmlNodeList elemList = doc.GetElementsByTagName("request");
foreach (XmlNode firstNode in elemList)
{
SecondTag = firstNode.ChildNodes;
foreach (XmlNode SecondNode in SecondTag)
{
if (SecondNode.Name.Equals("log-date"))
{
Line = SecondNode.InnerText + "|";
}
if (SecondNode.Name.Equals("service-name"))
{
Line = Line + SecondNode.InnerText + "|";
}
if (SecondNode.Name.Equals("request-id"))
{
Line = Line + SecondNode.InnerText + "|";
}
ThirdTag = SecondNode.ChildNodes;
foreach (XmlNode ThirdNode in ThirdTag)
{
FourthTag = ThirdNode.ChildNodes;
foreach (XmlNode FourthNode in FourthTag)
{
if (FourthNode.Name.Equals("GroupID"))
{
Line = Line + FourthNode.InnerText + "|";
}
if (FourthNode.Name.Equals("GroupName"))
{
Line = Line + FourthNode.InnerText + "|";
}
if (FourthNode.Name.Equals("ClientName"))
{
Line = Line + FourthNode.InnerText + "|";
}
FifthTag = FourthNode.ChildNodes;
foreach (XmlNode FifthNode in FifthTag)
{
if (FifthNode.Name.Equals("BrokerLoginName"))
{
BrokerLoginName = FifthNode.InnerText + "|";
}
if (FifthNode.Name.Equals("FullName"))
{
file.WriteLine(Line+BrokerLoginName+FifthNode.InnerText);
}
}
}
}
}
}
file.Close();
}

find and replace in file xml

i want open file xml and find string then replace.
but when replace string Only to find two strings and replace
this my code
var realpath = "~/template/xml/xmlback";
var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/template/xml/xmltest") + ".xml");
var filePath2 = Path.Combine(HttpContext.Current.Server.MapPath("~/template/xml/xmlback/test2") + ".xml");
DirectoryInfo dir = new DirectoryInfo(Path.Combine(HttpContext.Current.Server.MapPath(realpath)));
foreach (FileInfo files in dir.GetFiles())
{
files.Delete();
}
string strVal = System.IO.File.ReadAllText(Server.MapPath("~/template/xml/xmltest") + ".xml");
strVal = strVal.Replace("Test1", "amir");
strVal = strVal.Replace("Test2", "amir1");
strVal = strVal.Replace("Test3", "amir2");
strVal = strVal.Replace("Test4", "amir3");
strVal = strVal.Replace("Test5", "amir4");
strVal = strVal.Replace("Test6", "amir5");
File.Copy(Path.Combine(filePath), Path.Combine(filePath2));
System.IO.File.WriteAllText(Server.MapPath("~/template/xml/xmlback/test2") + ".xml", strVal);
ProcessRequest(filePath2, Lcourseid.Text);
im try open xml with word and see resultpic
I've solved my issue
iam use this code But no difference with the above code does not
I think it was a corrupted file xml,
Could not find Test3 and Test4,...
Thank you all
Dictionary<string, string> wordsToReplace = new Dictionary<string, string>();
wordsToReplace.Add("Test1", "amir");
wordsToReplace.Add("Test2", "amir1");
wordsToReplace.Add("Test3", "amir3");
wordsToReplace.Add("Test4", "amir4");
wordsToReplace.Add("Test5", "amir5");
wordsToReplace.Add("Test6", "amir6");
string strVal = System.IO.File.ReadAllText(Server.MapPath("~/template/xml/xmltest") + ".xml");
foreach (var pair in wordsToReplace)
{
//Performs each replacement
strVal = strVal.Replace(pair.Key, pair.Value);
}
File.Copy(Path.Combine(filePath), Path.Combine(filePath2));
System.IO.File.WriteAllText(Server.MapPath("~/template/xml/xmlback/test2") + ".xml", strVal);
ProcessRequest(filePath2, Lcourseid.Text);

parsing an xmldocument from a webrequest

I'm having a heck of a time parsing this layout:
<string xmlns="http://www.namespaceuri.com/Admin/ws">
<CardTrxSummary>
<PaymentMethod>
<Payment_Type_ID>VISA </Payment_Type_ID>
<Authorization>0.0000</Authorization>
<Capture>0.0000</Capture> <ForceCapture>0.0000</ForceCapture>
<PostAuth>0.0000</PostAuth> <Return>0.0000</Return>
<Sale>3419.2700</Sale> <Receipt>0.0000</Receipt>
<RepeatSale>0.0000</RepeatSale>
<Activate>0.0000</Activate>
<Deactivate>0.0000</Deactivate>
<Reload>0.0000</Reload>
<Authorization_Cnt>0</Authorization_Cnt>
<Capture_Cnt>0</Capture_Cnt>
<ForceCapture_Cnt>0</ForceCapture_Cnt>
<PostAuth_Cnt>0</PostAuth_Cnt>
<Return_Cnt>0</Return_Cnt>
<Sale_Cnt>13</Sale_Cnt>
<Receipt_Cnt>0</Receipt_Cnt>
<RepeatSale_Cnt>0</RepeatSale_Cnt>
<Activate_Cnt>0</Activate_Cnt>
<Deactivate_Cnt>0</Deactivate_Cnt>
<Reload_Cnt>0</Reload_Cnt>
<Cnt>13</Cnt>
</PaymentMethod>
</CardTrxSummary>
</string>
I am trying with this code to get the a specific result:
private static string ReadValueFromXml(XmlDocument xmlDocument, string field)
{
var xdoc = xmlDocument.ToXDocument();
var ns = "http://www.namespaceuri.com/Admin/ws";
return xdoc.Descendants(ns + "PaymentMethod")
.Select(x => (string) x.Attribute("Cnt"))
.FirstOrDefault();
}
At this point, it's giving me this message:
The ':' character, hexadecimal value 0x3A, cannot be included in a
name.
I tried it this way:
XmlNodeList xnList = xmlDocument.SelectNodes("/CardTrxSummary/PaymentMethod");
foreach (XmlNode xn in xnList)
{
Console.WriteLine("Sale: " + xn["Sale"].InnerText);
Console.WriteLine("Sale_Cnt: " + xn["Sale_Cnt"].InnerText);
Console.WriteLine("Payment_Type_ID: " + xn["Payment_Type_ID"].InnerText);
}
And it never went inside the foreach.
How do I get the values within PaymentMethod?
EDIT
I looked at the xmldocument's innertext and this is how it's displayed:
"<?xml version=\"1.0\" encoding=\"utf-8\"?><string xmlns=\"http://www.namespaceuri.com/Admin/ws\"><CardTrxSummary>\r\n <PaymentMethod>\r\n <Payment_Type_ID>VISA </Payment_Type_ID>\r\n <Authorization>0.0000</Authorization>\r\n <Capture>0.0000</Capture>\r\n <ForceCapture>0.0000</ForceCapture>\r\n <PostAuth>0.0000</PostAuth>\r\n <Return>0.0000</Return>\r\n <Sale>3419.2700</Sale>\r\n <Receipt>0.0000</Receipt>\r\n <RepeatSale>0.0000</RepeatSale>\r\n <Activate>0.0000</Activate>\r\n <Deactivate>0.0000</Deactivate>\r\n <Reload>0.0000</Reload>\r\n <Authorization_Cnt>0</Authorization_Cnt>\r\n <Capture_Cnt>0</Capture_Cnt>\r\n <ForceCapture_Cnt>0</ForceCapture_Cnt>\r\n <PostAuth_Cnt>0</PostAuth_Cnt>\r\n <Return_Cnt>0</Return_Cnt>\r\n <Sale_Cnt>13</Sale_Cnt>\r\n <Receipt_Cnt>0</Receipt_Cnt>\r\n <RepeatSale_Cnt>0</RepeatSale_Cnt>\r\n <Activate_Cnt>0</Activate_Cnt>\r\n <Deactivate_Cnt>0</Deactivate_Cnt>\r\n <Reload_Cnt>0</Reload_Cnt>\r\n <Cnt>13</Cnt>\r\n </PaymentMethod>\r\n</CardTrxSummary></string>"
Which, I assume, is part of my problem?
EDIT#2
This is what I ended up doing to get it to work. I'm sure there is a better way:
var tst2 = tst.InnerText.Replace("<", "<").Replace(">", ">").Replace("\r\n", string.Empty);
Console.WriteLine("Cnt: " + ReadXmlValue1(tst2, "Cnt"));
and my method to parse it:
private static void ReadXmlValue1(string xmlDocument)
{
XDocument xdoc = XDocument.Parse(xmlDocument);
//XNamespace ns = "http://www.namespaceuri.com/Admin/ws";
var payments = from p in xdoc.Descendants("PaymentMethod")
select new
{
Sale = (decimal)p.Element("Sale"),
SaleCount = (int)p.Element("Sale_Cnt"),
PaymentType = (string)p.Element("Payment_Type_ID")
};
Console.WriteLine("Count: " + payments.Count());
foreach (var payment in payments)
{
Console.WriteLine("Sale: " + payment.Sale);
Console.WriteLine("Sale_Cnt: " + payment.SaleCount);
Console.WriteLine("Payment_Type_ID: " + payment.PaymentType);
}
}
EDIT#3
This is how I'm creating the xmldocument:
/// <summary>
/// Get Data in xml format by url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static XmlDocument GetXmlDataFromUrl(string url)
{
//requesting the particular web page
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
//geting the response from the request url
var response = (HttpWebResponse)httpRequest.GetResponse();
//create a stream to hold the contents of the response (in this case it is the contents of the XML file
var receiveStream = response.GetResponseStream();
//creating XML document
var mySourceDoc = new XmlDocument();
//load the file from the stream
if (receiveStream != null)
{
mySourceDoc.Load(receiveStream);
//close the stream
receiveStream.Close();
return mySourceDoc;
}
return null;
}
You can use LINQ to XML to get list of strongly typed anonymous payment objects:
WebClient client = new WebClient();
string content = client.DownloadString(url);
XDocument xdoc = XDocument.Parse(content);
XNamespace ns = "http://www.namespaceuri.com/Admin/ws";
var payments = from p in xdoc.Descendants(ns + "PaymentMethod")
select new {
Sale = (decimal)p.Element(ns + "Sale"),
SaleCount = (int)p.Element(ns + "Sale_Cnt"),
PaymentType = (string)p.Element(ns + "Payment_Type_ID")
};
Keep in mind, that your xml has namespace declared, so you should provide it when specifying element names.
Usage:
foreach(var payment in payments)
{
Console.WriteLine("Sale: " + payment.Sale);
Console.WriteLine("Sale_Cnt: " + payment.SaleCount);
Console.WriteLine("Payment_Type_ID: " + payment.PaymentType);
}
XmlNode node = xmlDocument.SelectSingleNode("/string/CardTrxSummary/PaymentMethod");
Console.WriteLine("Sale: " + node.SelectSingleNode("Sale").InnerText);
Console.WriteLine("Sale_Cnt: " + node.SelectSingleNode("Sale_Cnt").InnerText);
Console.WriteLine("Payment_Type_ID: " + node.SelectSingleNode("Payment_Type_ID").InnerText);
or you can use getElementByTagName instead;
Console.WriteLine("Sale: " + xmlDocument.getElementByTagName("Sale").InnerText);
Console.WriteLine("Sale_Cnt: " + xmlDocument.getElementByTagName("Sale_Cnt").InnerText);
Console.WriteLine("Payment_Type_ID: " + xmlDocument.getElementByTagName("Payment_Type_ID").InnerText);
And just a note, the above 2 method is assuming that the tag will never return null.
If you want to handle possible null node, you can do something like this.
string text = xmlDocument.getElementByTagName("Sale") != null ? xmlDocument.getElementByTagName("Sale").InnerText : "unidentified";
The above line has the format like this:
var variable = condition ? A : B;
It's basically saying that if condition is true, variable equals A, otherwise variable equals B.

Can't create a file when users generate at a same time

I have a code to create an xml file on a server directory,
this code is working like a charm until users process this code at A SAME TIME.
the user who has a milliseconds faster will success generate a file and the other will losing it.
please help me guys.
public static void XmlOrder(arg_order mod, string NewAddress)
{
XmlDocument XDoc = new XmlDocument();
XmlDeclaration xde = XDoc.CreateXmlDeclaration("1.0", "", "");
XDoc.AppendChild(xde);
XmlElement XElemRoot = XDoc.CreateElement("Digital_Order");
XElemRoot.SetAttribute("xmlns", "");
XDoc.AppendChild(XElemRoot);
foreach (arg_order modDetail in mod.arg_orders) {
XmlElement Xsource = XDoc.CreateElement("Document");
XElemRoot.AppendChild(Xsource);
XmlElement XTemp = XDoc.CreateElement("wt_web_Id");
XTemp.InnerText = modDetail.order_detail_id.ToString();
Xsource.AppendChild(XTemp);
XTemp = XDoc.CreateElement("wt_addr");
XTemp.InnerText = modCompany.qad_no;
Xsource.AppendChild(XTemp);
}
//naming and path
string name = arg_order_detail.FindAll().Count.ToString() + ".xml";
string path = ArgenXmlOrderPath + "/" + name;
XDoc.Save(path);
}
That is cause your users are trying to write the same file at the same time.
Make your file names unique by adding something like the orderID to the name of the file.
string name = modDetail.order_detail_id.ToString() + ".xml";
string path = ArgenXmlOrderPath + "/" + name;
XDoc.Save(path);

Confusion in Combining 4 Functions

please bear with me if my codes are messy or bulky, I am a real beginner which started programming a month ago with no background at all. I now have 4 functions created searchComByAuthor();, searchComByStartDate();, searchComByEndDate(); and searchComByKeywords();. But the problem is, I don't know how to combine them and make them like filters. User can choose to fill up any textboxes and when the user clicks on "Analyse" button, the combined function will do the work. Now they are all working separately, I only call the functions one by one to test out if they work or not.
Screenshot:
searchComByAuthor:
private void searchComByAuthor()
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
try
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
string docPath = fileName;
xmlDoc.Load(docPath); //* load the XML document from the specified file.
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in nodeList)
{
XmlElement itemElement = (XmlElement)node;
string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
if (itemAuthor.ToLower() == txtComAuthor.Text.ToString().ToLower())
{
string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);
richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
"\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
}
//else
//{
// richComResults.AppendText("There is no author " + txtComAuthor.Text.ToString().ToLower() + ". Please ensure you are using a correct author name.");
//}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
int pointer = 0;
int index = 0;
string keyword = txtComAuthor.Text;
string shadow = richComByTemplate.Text.ToLower();
while (true)
{
//Searching in the copy/shadow
index = shadow.IndexOf(keyword, pointer);
//if keyword not found then the loop will break
if ((index == -1) || (String.IsNullOrEmpty(keyword)))
{
break;
}
richComByTemplate.Select(index, keyword.Length);
richComByTemplate.SelectionColor = Color.Red;
richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
pointer = index + keyword.Length;
}
}
searchComByStartDate:
private void searchComByStartDate()
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
string docPath = fileName;
xmlDoc.Load(docPath); //* load the XML document from the specified file.
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in nodeList)
{
XmlElement itemElement = (XmlElement)node;
string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
CultureInfo provider = CultureInfo.InvariantCulture;
if (DateTime.Parse(itemDate) >= DateTime.ParseExact(txtComStartDate.Text, "dd/MM/yy", provider))
{
string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);
richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
"\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
}
}
}
}
searchComByEndDate:
private void searchComByEndDate()
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
string docPath = fileName;
xmlDoc.Load(docPath); //* load the XML document from the specified file.
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in nodeList)
{
XmlElement itemElement = (XmlElement)node;
string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
CultureInfo provider = CultureInfo.InvariantCulture;
if (DateTime.Parse(itemDate) <= DateTime.ParseExact(txtComEndDate.Text, "dd/MM/yy", provider))
{
string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);
richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
"\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
}
}
}
}
searchComByKeywords:
private void searchComByKeywords()
{
List<TextBox> boxes = new List<TextBox>();
boxes.Add(txtComKeyword1);
boxes.Add(txtComKeyword2);
boxes.Add(txtComKeyword3);
boxes.Add(txtComKeyword4);
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
try
{
XmlDocument xmlDoc = new XmlDocument(); //* create an xml document object.
string docPath = fileName;
xmlDoc.Load(docPath); //* load the XML document from the specified file.
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("item");
foreach (XmlNode node in nodeList)
{
XmlElement itemElement = (XmlElement)node;
string itemDescription = itemElement.GetElementsByTagName("description")[0].InnerText;
if (txtComKeyword1.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword1.Text.ToLower()) ||
txtComKeyword2.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword2.Text.ToLower()) ||
txtComKeyword3.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword3.Text.ToLower()) ||
txtComKeyword4.Text != (String.Empty) && itemDescription.ToLower().Contains(txtComKeyword4.Text.ToLower()))
{
string itemTitle = itemElement.GetElementsByTagName("title")[0].InnerText;
string itemDate = itemElement.GetElementsByTagName("pubDate")[0].InnerText;
string itemAuthor = itemElement.GetElementsByTagName("author")[0].InnerText;
string itemXMLFile = Path.GetFileNameWithoutExtension(fileName);
richComByTemplate.AppendText("SYMBOL: " + itemXMLFile + "\nAUTHOR: " + itemAuthor + "\nDATE: " + itemDate +
"\nTITLE: " + itemTitle + "\nDESCRIPTION: " + itemDescription + "\n\n--------\n\n");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
foreach (TextBox box in boxes)
{
int pointer = 0;
int index = 0;
string keyword = box.Text;
string shadow = richComByTemplate.Text.ToLower();
while (true)
{
//Searching in the copy/shadow
index = shadow.IndexOf(keyword, pointer);
//if keyword not found then the loop will break
if ((index == -1) || (String.IsNullOrEmpty(keyword)))
{
break;
}
//Customising the original data
richComByTemplate.Select(index, keyword.Length);
richComByTemplate.SelectionColor = Color.Red;
richComByTemplate.SelectionFont = new System.Drawing.Font(richComByTemplate.Font, FontStyle.Bold);
pointer = index + keyword.Length;
}
}
}
You have a very significant amount of repeated code in your functions.
You can achieve your goal while simplifying your code by combining your four functions into a single function. Give that single function a parameter that is a list of conditions that must be met in order to include a given record (assuming all conditions must apply to select the record).
In your case it looks like you desire a straight match on whatever is entered in one or more text fields. Pass in the values of those text fields to your new, combined method (so that you can separate your logic from your UI) and, for each value that has a non-null, non-blank value, apply the appropriate logic test. If Author and StartDate are passed in, apply the logic that is relevant to both Author and StartDate. If both tests pass, include that record.
In order to "include that record", rather than doing what the code is currently doing:
richComByTemplate.AppendText(...)
you may wish to return a list of results and have the caller do the text appending (again, to separate your user interface from your search logic).

Categories