Fulltext Search On XML Files using XDocument
Given the following XML-Document:
<Customers>
<Customer>
<Name>Jerry</Name>
<Age>26</26>
<Addresses>
<Address>
<Street>Main Street</Street>
<Address>
<Addresses>
<Customers>
1) How would you implement a fulltext search to return a List of Customer-Names?
public void StartRead(string searchTerm)
{
XDocument xDocument = XDocument.Parse(GetFile());
//get all retval-nodes (allthough it’s just 1)
var customers = from cmd in xDocument.Descendants(“Customer”)
select cmd;
//loop through retval-nodes (still only 1)
foreach (XElement cust in customers)
{
if (RecursiveParser(cust, searchTerm))
{
this.textBox1.Text += cust.Element(“Name”).Value + @” ### “;
}
else
{
this.textBox1.Text += @”empty ### “;
}
}
}
public bool RecursiveParser(XElement element, string searchTerm)
{
if (element.IsEmpty) return false;
if (element.HasElements)
{
foreach (XElement xElement in element.Elements())
{
if (xElement.Value.ToLower().Contains(searchTerm))
return true;
if (RecursiveParser(xElement, searchTerm))
return true;
}
}
return false;
}
2. How would you implement a fulltext search for addresses only?
Just replace “Customer” by “Address”:
//get all retval-nodes (allthough it’s just 1)
var customers = from cmd in xDocument.Descendants(“Address”)
select cmd;
Given the following XML-Document (it’s the output of a serialized List<Customer>):
<ArrayOfCustomer>
<Customer>
<Name>Jerry</Name>
<Age>26</Age>
<Addresses>
<Address>
<Street>Main Street</Street>
</Address>
</Addresses>
</Customer>
</ArrayOfCustomer>
1) How would you implement a fulltext search to return a List of Customer-Names?
(weiterlesen…)