What is the fastest method to retrieve an XML node? I have an application that need the functionality of replace a specific node, when the document is small is fast but soon get bigger then it takes some several second to do the replacement. This is the method, I just do a brute force comparison that sucks real bad in that scenario.
public bool ReplaceWithAppendFile(string IDReplace)
{
XElement UnionElement = (from sons in m_ExtractionXmlFile.Root.DescendantsAndSelf()
where sons.Attribute("ID").Value == IDReplace
select sons).Single();
UnionElement.ReplaceWith(m_AppendXmlFile.Root.Elements());
m_ExtractionXmlFile.Root.Attribute("MaxID").Value =
AppendRoot.Attribute("MaxID").Value;
if (Validate(m_ExtractionXmlFile, ErrorInfo))
{
m_ExtractionXmlFile.Save(SharedViewModel.ExtractionFile);
return true;
}
else
{
m_ExtractionXmlFile = XDocument.Load(SharedViewModel.ExtractionFile);
return false;
}
}
Try using XPath:
You may refer to Finding Elements by Attributes in a DOM Document Using XPath for more examples.
P.S. It is considered good convention to start names of parameters and local variables in lowercase. Thus, use
idReplaceandunionElementrather than the above.