upb

xml embed references. halp! :(

Rate this Entry
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 ...

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

Submit "xml embed references. halp! :(" to Digg Submit "xml embed references. halp! :(" to del.icio.us Submit "xml embed references. halp! :(" to StumbleUpon Submit "xml embed references. halp! :(" to Google

Categories
Uncategorized

Comments