private static XmlNamespaceManager GetNamespaceManager(XmlNode node, XmlDocument document) {
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(document.NameTable);
List<XmlElement> elements = new List<XmlElement>();
while (node != null) {
XmlElement element = node as XmlElement;
if (element != null
&& element.HasAttributes) {
elements.Add(element);
}
node = node.ParentNode;
}
for (int i = elements.Count - 1; i >= 0; i--) {
namespaceManager.PushScope();
XmlAttributeCollection attributes = elements[i].Attributes;
for (int j = 0; j < attributes.Count; j++) {
XmlAttribute attribute = attributes[j];
if (attribute.IsNamespace) {
string prefix = attribute.Prefix.Length == 0 ? string.Empty : attribute.LocalName;
namespaceManager.AddNamespace(prefix, attribute.Value);
}
}
}
return namespaceManager;
}