OK, my brain just isn't functioning right today and I've spent too much time on this.
I have some XML that looks like this...
I have an XPathNavigator pointing to /root/element/child (call it nodeNav). I want to 'dedupe' the child elements... at the end, I want something that looks like this:
(the order of the values of the child/entry nodes does not matter in the final output).
I've tried stuff like this:
with no luck so far. The node iterator inside the loop always returns a .Count = 0. Basically, I'm looking for a query that will return the navigators to all the ndoes of the parent nodeNav that have the same .Value. There may be a better query from the childElement's standpoint (give me all my siblings, including myself, that have the same text()) but I haven't looked down that path yet.
I have some XML that looks like this...
Code:
<root>
<element>
<child>
<entry>value1</entry>
<entry>value1</entry>
<entry>value2</entry>
<entry>value3</entry>
<entry>value1</entry>
<entry>value3</entry>
<entry>value2</entry>
<entry>value2</entry>
<entry>value3</entry>
<entry>value1</entry>
</child>
</element>
</root>
I have an XPathNavigator pointing to /root/element/child (call it nodeNav). I want to 'dedupe' the child elements... at the end, I want something that looks like this:
Code:
<root>
<element>
<child>
<entry>value1</entry>
<entry>value2</entry>
<entry>value3</entry>
</child>
</element>
</root>
(the order of the values of the child/entry nodes does not matter in the final output).
I've tried stuff like this:
Code:
private static void Dedupe(XPathNavigator nodeNav)
{
int index = 1;
const string childQueryTemplate = "./*[position() = {0}]";
string childQuery = string.Format(childQueryTemplate, index);
XPathNavigator childElement = nodeNav.SelectSingleNode(childQuery);
while (childElement != null)
{
const string childValueQueryTemplate = "./*[text() = ./[{0}]/text()]";
string query = string.Format(childValueQueryTemplate, index);
XPathNodeIterator ni = nodeNav.Select(query);
if (ni.Count > 1)
childElement.DeleteSelf();
else
index++;
childElement = nodeNav.SelectSingleNode(string.Format(childQueryTemplate, index));
}
}
with no luck so far. The node iterator inside the loop always returns a .Count = 0. Basically, I'm looking for a query that will return the navigators to all the ndoes of the parent nodeNav that have the same .Value. There may be a better query from the childElement's standpoint (give me all my siblings, including myself, that have the same text()) but I haven't looked down that path yet.