次のようなXMLドキュメントがあります
<recipes>
<ingredient value"1">APPLE</ingredient>
<ingredient value"2">BANANA</ingredient>
<ingredient value"3">APPLE ORANGE</ingredient>
<ingredient value"4">APPLE BANANA</ingredient>
<ingredient value"5">APPLE STRAWBERRY</ingredient>
<ingredient value"6">GRAPES</ingredient>
</recipes>
ここで、ユーザーは文字列を入力しますApple Grapes Banana
。アルファベット順に並べ替え、文字列操作を使用して最後の単語を再帰的に削除することにより、値の 1 つと一致させようとします。しかし、Linq でこれを行うより効率的な方法があると確信しています。XML で最も近い一致を返すクエリが必要です<ingredient value"4">APPLE BANANA</ingredient>
。
string str = "APPLE BANANA GRAPES"; // user input arranged by ascending alphabet and capitalized
XDocument xdoc = XDocument.Load(above xml);// gets above xml
var h = xdoc.Root.Elements("ingredient").FirstOrDefault(u => u.Value == str);//recurse these steps
if (h == null)
{
str = str.Remove(str.LastIndexOf(" "));//recurse these steps
}
//check if str matches any value;
//if not remove last word from str and check again;