Is there a way to improve upon this? I feel like I have way to many loops going on.
The code builds a string with domain names in it from an XML document. Depending on whether the domain name in the XML doc is in the hostSW (host Starts With), hostCN (host Contains) or hostEW (host EndsWith) element depends on whether I need to append a * to the end, beginning+end, or beginning of the value respectively.
I used a Hashset as a way of making sure that there is no duplication.
var startWith = xdoc.Root
.Descendants("Test")
.Elements("hostSW")
.ToList();
var contains = xdoc.Root
.Descendants("Test")
.Elements("hostCN")
.ToList();
var endsWith = xdoc.Root
.Descendants("Test")
.Elements("hostEW")
.ToList();
HashSet<string> domains = new HashSet<string>(); //use hashset so we don't duplicate results
foreach (XElement test in startWith)
{
domains.Add(test.Value.ToString() + "*");
}
foreach (XElement test in contains)
{
domains.Add("*" + test.Value.ToString() + "*");
}
foreach (XElement test in endsWith)
{
domains.Add("*" + test.Value.ToString());
}
string out = "BEGIN FILE ";
foreach (string domain in domains.ToArray())
{
out += "BEGIN DOMAIN ";
out += domain;
out += " END DOMAIN";
}
out += " END FILE;
return out;
XML file
<Tests>
<Test>
<hostSW>startsWith1</hostSW>
</Test>
<Test>
<hostSW>startsWith2</hostSW>
</Test>
<Test>
<hostCN>contains1</hostCN>
</Test>
<Test>
<hostEW>endsWith1</hostEW>
</Test>
</Tests>
Assuming an XML structure like this
<Root>
<Test>
<hostSW>startsWith1</hostSW>
</Test>
<Test>
<hostCN>contains1</hostCN>
</Test>
<Test>
<hostCN>contains2</hostCN>
</Test>
<Test>
<hostEW>endsWith1</hostEW>
</Test>
<Test>
<hostEW>endsWith2</hostEW>
</Test>
<Test>
<hostEW>endsWith3</hostEW>
</Test>
</Root>
I got all the domains correctly and only iterated 6 times with this code
// Loop each of the elements one time without filtering element name
foreach (var test in xDoc.Root.Descendants("Test").Elements())
{
// Switch on the name of the element.
switch (test.Name.LocalName)
{
case "hostSW": { domains.Add(test.Value + "*"); } break;
case "hostCN": { domains.Add("*" + test.Value + "*"); } break;
case "hostEW": { domains.Add("*" + test.Value); } break;
}
// For any other elements we iterate just do nothing
}