我在一个外部系统写入 xml(没有定义的命名空间)的项目中工作,我们需要读取它们以查找具有一些特殊值的节点:
- 当值没有准备好时,我们在几分钟后再次读取。
- 在其他情况下,我们处理节点(属性、值等)
所以,我认为这段代码可以帮助你:
var input1 = @"<root>
<ta>
<XGLi6id90>774825484.1418393</XGLi6id90>
<VAfrBVB>
<EG60sk>1030847709.7303829</EG60sk>
<XR>NOT_READY</XR>
</VAfrBVB>
</ta>
<DxshpR>1123</DxshpR>
var input2 = @"<root>
<ta>
<XGLi6id90>774825484.1418393</XGLi6id90>
<VAfrBVB>
<EG60sk>1030847709.7303829</EG60sk>
<XR>99999999</XR>
</VAfrBVB>
</ta>
<DxshpR>1123</DxshpR>
var stream1 = new MemoryStream(Encoding.UTF8.GetBytes(input1));
var stream2 = new MemoryStream(Encoding.UTF8.GetBytes(input2));
stream1.Position = 0;
stream2.Position = 0;
var position1 = DoWork(stream1, new Position());
var position2 = DoWork(stream2, position1);
public static Position DoWork(Stream stream, Position position)
{
using (XmlTextReader xmlTextReader = new XmlTextReader(stream))
{
using (XmlReader xmlReader = XmlReader.Create(xmlTextReader, xmlReaderSettings))
{
// restores the last position
xmlTextReader.SetPosition(position);
System.Diagnostics.Debug.WriteLine(xmlReader.Value); // Second time prints 99999999
while (xmlReader.Value != "NOT_READY" && xmlReader.Read())
{
// a custom logic to process nodes....
}
// saves the position to process later ...
position = xmlTextReader.GetPosition();
System.Diagnostics.Debug.WriteLine(xmlReader.Value); // First time prints NOT_READY
}
}
return position;
}
}
public class Position
{
public int LinePosition { get; set; }
public int LineNumber { get; set; }
}
public static class XmlReaderExtensions
{
public static void SetPosition(this XmlTextReader xmlTextReader, Position position)
{
if (position != null)
{
while (xmlTextReader.LineNumber < position.LineNumber && xmlTextReader.Read())
{
}
while (xmlTextReader.LinePosition < position.LinePosition && xmlTextReader.Read())
{
}
}
}
public static Position GetPosition(this XmlTextReader xmlTextReader)
{
Position output;
if (xmlTextReader.EOF)
{
output = new Position();
}
else
{
output = new Position { LineNumber = xmlTextReader.LineNumber, LinePosition = xmlTextReader.LinePosition };
}
return output;
}
}
重要且明显的是,只有当 xml 的结构(换行符、节点等)始终相同时,它才会起作用。在其他情况下,它将不起作用。