好的,这里有一些需要考虑的注意事项。 XMLReader
从某种意义上说,我明白你使用它(没有代码示例)是你遍历文档,因为XMLReader
是只向前和只读的。
因此,您需要迭代,直到找到所需的节点。在下面的例子中,我找到标有“work”的地址元素并提取整个节点。然后根据需要在此节点上查询。
using (var inFile = new FileStream(path, FileMode.Open))
{
using (var reader = new XmlTextReader(inFile))
{
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "Address" && reader.GetAttribute(0) == "Work")
{
// Create a document, which will contain the address element as the root
var doc = new XmlDocument();
// Create a reader, which only will read the substree <Address> ... until ... </Address>
doc.Load(reader.ReadSubtree());
// Use XPath to query the nodes, here the "Name" node
var name = doc.SelectSingleNode("//Address/Name");
// Print node name and the inner text of the node
Console.WriteLine("Node: {0}, Inner text: {1}", name.Name, name.InnerText);
}
break;
}
}
}
}
编辑
作出不使用LINQ
你能后你有什么到目前为止已经试过? (我假设你正在使用C#) – aweis 2012-03-01 15:02:39