通过 ID 检索 Xml 节点的最快方法是什么
检索 XML 节点最快的方法是什么?我有一个应用程序需要替换特定节点的功能,当文档很小时速度很快,但很快就会变大,然后需要几秒钟的时间才能进行替换。这就是方法,我只是做了一个蛮力比较,在这种情况下效果很糟糕。
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用XPath:
您可以参考Finding Elements by Attributes in a DOM Document using XPath 了解更多示例。
PS 参数和局部变量的名称以小写开头被认为是良好的约定。因此,请使用
idReplace
和unionElement
而不是上面的。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
idReplace
andunionElement
rather than the above.