Tag: xpath

在C#中比较两个XML文件并使用XMLDiff生成第三个XML文件

我正在尝试编写一个简单的算法来读取具有完全相同的节点和结构的两个XML文件,但不一定是子节点内的相同数据而不是相同的顺序。 我如何使用Microsoft的XML Diff .DLL创建一个简单的实现来创建第三个临时XML,即两个第一个之间的差异? MSDN上的XML Diff: XML差异和补丁工具 XML Diff和Patch GUI工具 示例要比较的两个不同XML文件的XML代码: Sidney Crosby PIT C 39 32 33 20 29 Sidney Crosby PIT C 42 35 34 22 30 想要的结果(两者之间的差异) Sidney Crosby PIT C 3 3 1 2 1 在这种情况下,我可能会使用XSLT将生成的XML“差异”文件转换为已排序的HTML文件,但我还没有。 我想要做的就是在第三个XML文件中显示每个节点的每个数值的差异,从“GP”子节点开始。 到目前为止C#代码: private void CompareXml(string file1, string file2) { XmlReader reader1 = XmlReader.Create(new StringReader(file1)); XmlReader […]

如何使用choicesinglenode()访问具有属性和命名空间的xml节点

我有这个文件,我希望得到”x_server_response/retrieve_resources_by_category_response/source_full_info/record/ datafield[@tag=’520′]/subfield[@code=’a’]”但我不能! 为什么? 我怀疑这与记录节点上的命名空间daclaration有关。 但我无法弄清楚如何做到这一点。 我的代码看起来像这样: XmlNodeList xmlResources = r.ResponseXmlDocument.SelectNodes(“x_server_response/retrieve_resources_by_category_response/source_full_info); foreach (XmlNode xmlResource in xmlResources) { string information = xmlResource.SelectSingleNode(“record/datafield[@tag=’520′]/subfield[@code=’a’]”).InnerText; 而xml是这样的: metalib_version=”4.00 (20)> CKB02166 Providing access to thousands of online journals from leading scholarly, academic and business publishers, the Ingenta Select service provides fast and reliable access from a global network of servers to users’ […]

HtmlAgilityPack DocumentNode.SelectNodes返回null,不应该

我正在尝试使用HTML敏捷包从示例页面中删除内容。 当我认为它不应该时,DocumentNode.SelectNodes为XPath查询返回null。 有人能告诉我为什么吗? 代码是: HtmlDocument doc = new HtmlDocument(); string xpath = “//h1[@class=’product-title fn’]”; // note, it still returns // null even with “//div” doc.OptionFixNestedTags = true; HtmlNode.ElementsFlags.Remove(“form”); HtmlNode.ElementsFlags.Remove(“option”); HtmlNodeCollection coll = doc.DocumentNode.SelectNodes(xpath); if (coll != null) { // do stuff } else { // not expecting it to be null unless no matches }

XPath:如何按属性选择节点?

我有一个像这样的XML: #FF0000 #FF0200 #FF0300 #FF0500 [..] 我正在尝试按索引选择节点: XmlDocument ColorTable = new XmlDocument(); ColorTable.Load(HttpContext.Current.Server.MapPath(“~/App_Data/ColorTable.xml”)); int percentage = 2; string xpath = string.Format(@”//color[index={0}]”, percentage.ToString()); //string xpath = string.Format(@”//color[index=””{0}””]”, percentage.ToString()); //string xpath = string.Format(@”//color[index='{0}’]”, percentage.ToString()); var r = ColorTable.SelectSingleNode(xpath).Value; 我也尝试了注释版本,但它没有返回任何结果。 有什么建议吗?

如何在C#中选择带XPath的节点?

简单的问题,我只想从标签中选择文本。 这就是我所拥有的,但Xpath与任何东西都不匹配。 public static void TestXPath() { string xmlText = “”; xmlText += “”; xmlText += “Normal 1 1 6”; xmlText += “”; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(new System.IO.StringReader(xmlText)); foreach (XmlNode node in xmlDoc.SelectNodes(“//Template”)) { Console.WriteLine(“{0}: {1}”, node.Name, node.InnerText); } }

删除没有子节点的父节点

我有一个与从xml文件中删除特定节点有关的问题。 这是我的XML示例: 我想删除节点nodeB=”toRemove”而不删除此节点的子节点。 之后我需要使用nodeB attribute=”placeHolder”做同样的事情。 结果的一部分看起来像这样: 我一直在尝试这样的代码来实现: XmlNodeList nodeList = doc.SelectNodes(“//nodeB[@attribute=\”toRemove\”]”); foreach (XmlNode node in nodeList) { foreach (XmlNode child in node.ChildNodes) { node.ParentNode.AppendChild(child); } node.ParentNode.RemoveChild(node); } doc.Save(XmlFilePathSource); 我能够找到具有所需属性toRemove或placeHolder的节点,但是我无法将此节点的子节点向上移动一级。 在这种情况下你能帮我吗? 它可以是Linq,XDocument,XmlReader的解决方案,但我更喜欢使用XmlDocument。 感谢您提前给我提供的任何帮助。 编辑: 在这种情况下,我使用了稍微修改过的代码(以保持顺序),Chuck Savage写道。 一旦删除 然后再做同样的事情 这是稍微修改过的代码 XElement root = XElement.Load(XmlFilePathSource); var removes = root.XPathSelectElements(“//nodeB[@attribute=\”toRemove\”]”); foreach (XElement node in removes.ToArray()) { node.Parent.AddAfterSelf(node.Elements()); node.Remove(); } […]

XPath在C#中无法正常工作

我的代码没有返回节点 XmlDocument xml = new XmlDocument(); xml.InnerXml = text; XmlNode node_ = xml.SelectSingleNode(node); return node_.InnerText; // node_ = null ! 我很确定我的XML和Xpath是正确的。 我的Xpath: /ItemLookupResponse/OperationRequest/RequestId 我的XML: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx 由于某种原因,我的XPath返回的节点始终为null。 有人可以帮忙吗?

如何最好地在.NET中使用XPath和非常大的XML文件?

我需要在C#中对相当大的XML文件(这里可能超过一千兆字节)进行一些处理,包括执行一些复杂的xpath查询。 我遇到的问题是我通常通过System.XML库执行此操作的标准方法是在将任何内容添加到内存之前将整个文件加载到内存中,这会导致此大小的文件出现内存问题。 我只需要阅读它们并查询其中包含的数据就不需要更新文件。 一些XPath查询非常复杂,并且涉及多个级别的父子类型关系 – 我不确定这是否会影响使用流式读取器的能力,而不是将数据作为块加载到内存中。 我可以看到使其工作的一种方法是使用基于流的方法执行简单分析,并且可能将XPath语句包装到XSLT转换中,之后我可以在文件中运行,尽管看起来有点复杂。 或者我知道XPath查询有一些元素不能运行,所以我想我可以根据它的原始树结构将文档分解为一系列较小的片段,这可能足够小,可以在内存中处理造成太大的破坏。 我试图在这里解释我的目标,所以如果我在一般方法上咆哮完全错误的树,我相信大家可以让我正确…

用于更改链接的HtmlAgilityPack示例不起作用。 我该如何做到这一点?

codeplex的例子是这样的: HtmlDocument doc = new HtmlDocument(); doc.Load(“file.htm”); foreach(HtmlNode link in doc.DocumentElement.SelectNodes(“//a[@href”]) { HtmlAttribute att = link[“href”]; att.Value = FixLink(att); } doc.Save(“file.htm”); 第一个问题是HtmlDocument。 DocumentElement不存在! 存在的是HtmlDocument。 DocumentNode,但即使我使用它,我也无法按照描述访问href属性。 我收到以下错误: Cannot apply indexing with [] to an expression of type ‘HtmlAgilityPack.HtmlNode’ 当我收到此错误时,这是​​我正在尝试编译的代码: private static void ChangeUrls(ref HtmlDocument doc) { foreach(HtmlNode link in doc.DocumentNode.SelectNodes(“//@href”)) { HtmlAttribute attr = link[“href”]; attr.Value […]

将XPath与XML命名空间一起使用

我想用XPath处理下面的xml: Tere是根元素的xmlns 。 我的代码是这样的: XElement doc = XElement.Load(xmlFilePath); XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace(“prefix”, “http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration”); XElement settingDiagConnectionString = doc.XPathSelectElement(“//prefix:ServiceConfiguration/Role/ConfigurationSettings/Setting[1]”, ns); // <===== always return null. settingDiagConnectionString.SetAttributeValue("value", "hello, world!"); 但是settingDiagConnectionString始终为null。 为什么? 加1 谢谢har07。 为每个元素添加前缀后,以下代码有效: XmlDocument doc = new XmlDocument(); doc.Load(cscfgPath); XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace(“prefix”, “http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration”); XmlNode settingDiagConnectionString = doc.SelectNodes(“/prefix:ServiceConfiguration/prefix:Role/prefix:ConfigurationSettings/prefix:Setting[1]”, ns)[0]; settingDiagConnectionString.Attributes[“value”].Value = […]