upb
November 8th, 2007, 01:00
Hi.
Could anyone please offer any tips/pointers/ideas on how to optimize the following:
For each element in document, that has a href attribute and no children elements and no text children nodes,
find the element with id = the value of that href attribute and move all children elements and attributes (except id) to the referencing element, and remove the referenced element.
The current implementation takes 0,09 sec per root level detached element on a 10MB xml document, which amounts to about 3 minutes.
That is bearable but the situation is hopeless with more elements ...
thx to anyone who takes a look
Could anyone please offer any tips/pointers/ideas on how to optimize the following:
For each element in document, that has a href attribute and no children elements and no text children nodes,
find the element with id = the value of that href attribute and move all children elements and attributes (except id) to the referencing element, and remove the referenced element.
The current implementation takes 0,09 sec per root level detached element on a 10MB xml document, which amounts to about 3 minutes.
That is bearable but the situation is hopeless with more elements ...
Code:
protected static void EmbedRefs(ref XmlNode n, ref XmlNode container)
{
int i = 0;
XmlNodeList detachedChildren = n.SelectNodes("child::*[@href and not(text())]";
foreach (XmlNode child in detachedChildren)
{
string href = child.Attributes["href"].Value.Substring(1);
XmlNode referee = container.SelectSingleNode("child::*[@id='" + href + "']";
EmbedRefs(ref referee, ref container);
child.Attributes.RemoveNamedItem("href";
while (referee.FirstChild != null)
{
XmlNode fc = referee.FirstChild;
referee.RemoveChild(fc);
child.AppendChild(fc);
}
foreach (XmlAttribute attr in referee.Attributes)
{
if (attr.LocalName == "id"
continue;
child.Attributes.Append((XmlAttribute)attr.Clone());
}
referee.ParentNode.RemoveChild(referee);
}
}
thx to anyone who takes a look