本文介绍了具有连续属性的连续节点检查程序中的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试找到一些连续的节点< xref ref-type =bibrrid =ref ...> ...< / xref> (当 3 或更多时)文件中用逗号或空格分隔并将它们写入日志文件。 注意:我正在尝试识别的连续节点的各自的rid值应增加 +1 减去文本 ref 。这是一个小样本文件 https://codeshare.io/5wOjlK 并且所需的输出是 I'm trying to find some consecutive nodes <xref ref-type="bibr" rid="ref...">...</xref> (when there are 3 or more) in a file that are separated by a comma or space and write them to a log file.NOTE: The consecutive nodes that I'm trying to identify should have their respective rid values incremented by +1 minus the text ref. Here is small sample file https://codeshare.io/5wOjlKand the desired output is<xref ref-type="bibr" rid="ref2">[2]</xref>, <xref ref-type="bibr" rid="ref3">[3]</xref>, <xref ref-type="bibr" rid="ref4">[4]</xref> <xref ref-type="bibr" rid="ref11">[11]</xref>, <xref ref-type="bibr" rid="ref12">[12]</xref> <xref ref-type="bibr" rid="ref13">[13]</xref> 这里是我正在使用的代码 https://codeshare.io/ar6mPA 但它显示了dtd未找到的类型错误,我该如何忽略..我尝试使用下面的代码 我的尝试: here is the code that I'm using https://codeshare.io/ar6mPA But it shows a dtd not found type error, how do I ignore that..I tried using the below codeWhat I have tried:FileStream xmlStream = new FileStream(@"D:\test\12345.XML", FileMode.Open, FileAccess.Read);XmlReaderSettings settings = new XmlReaderSettings();settings.XmlResolver = null;settings.ProhibitDtd = false;XmlReader reader = XmlTextReader.Create(xmlStream, settings);XmlDocument doc = new XmlDocument();doc.Load(reader); 而不是 instead ofXmlDocument doc = new XmlDocument();doc.PreserveWhitespace = true;doc.Load(@"D:\test\12345.XML"); 但现在只显示第一场比赛......我很困惑..任何人都可以帮忙... But now it is showing only the first match...I'm confused.. Can anyone help please...推荐答案 我更喜欢使用 XDocument类 [ ^ ]当需要实现自定义搜索方法时,它非常灵活。请参阅: I prefer to use XDocument class[^] which is very "flexible" when there's a need to implement custom search method. See:XmlReaderSettings settings = new XmlReaderSettings();settings.DtdProcessing = DtdProcessing.Parse;XDocument xdoc = XDocument.Load(XmlReader.Create("fullfilename.xml", settings));var cons = xdoc.Descendants("xref") .GroupBy(x=>x.Parent) .Select(grp=> new { Parent = grp.Key, ConsecutiveNodes = grp.Select((n, i)=> new { Index = i+1, Node = n }), Count = grp.Count() }) .ToList();Console.WriteLine("3 or more consecutive nodes:");foreach(var o in cons){ if (o.Count>2) { Console.WriteLine("{0}", new string('=', 30)); Console.WriteLine("Found in: {0} ... {1}", o.Parent.ToString().Substring(0,15), o.Parent.ToString().Substring(o.Parent.ToString().Length-15,15)); Console.WriteLine("{0}", new string('-', 50)); foreach (var c in o.ConsecutiveNodes) { //Console.WriteLine("{0}", c.Node); Console.WriteLine("Original rid value [{0}] will be replaced with [{1}]", c.Node.Attribute("rid").Value, c.Index); c.Node.Attribute("rid").Value = c.Index.ToString(); } }} 以上代码显示: Above code displays:3 or more consecutive nodes:==============================Found in: <p>In this stud ... 15]</xref>.</p>--------------------------------------------------Original rid value [ref2] will be replaced with [1]Original rid value [ref3] will be replaced with [2]Original rid value [ref4] will be replaced with [3]Original rid value [ref20] will be replaced with [4]Original rid value [ref3] will be replaced with [5]Original rid value [ref15] will be replaced with [6]==============================Found in: <p>The measurin ... cattering..</p>--------------------------------------------------Original rid value [ref11] will be replaced with [1]Original rid value [ref12] will be replaced with [2]Original rid value [ref13] will be replaced with [3]Original rid value [ref4] will be replaced with [4]Original rid value [T2] will be replaced with [5] 如需了解更多信息,请参阅: XDocument.Load方法(XmlReader)(System.Xml.Linq) [ ^ ] XmlReaderSettings.DtdProcessing Property(System.Xml) [ ^ ] 随意更改代码以满足您的需求。祝你好运!For further information, please see:XDocument.Load Method (XmlReader) (System.Xml.Linq)[^]XmlReaderSettings.DtdProcessing Property (System.Xml)[^]Feel free to change code to your needs. Good luck! 这篇关于具有连续属性的连续节点检查程序中的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 01:16