The sample code in the MSDN documentation for the XNode.ReadFrom method is as follows:
class Program { static IEnumerable<XElement> StreamRootChildDoc(string uri) { using (XmlReader reader = XmlReader.Create(uri)) { reader.MoveToContent(); // Parse the file and display each of the nodes. while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (reader.Name == "Child") { XElement el = XElement.ReadFrom(reader) as XElement; if (el != null) yield return el; } break; } } } } static void Main(string[] args) { IEnumerable<string> grandChildData = from el in StreamRootChildDoc("Source.xml") where (int)el.Attribute("Key") > 1 select (string)el.Element("GrandChild"); foreach (string str in grandChildData) Console.WriteLine(str); } }
But I found that the StreamRootChildDoc method in this example should be changed as follows:
static IEnumerable<XElement> StreamRootChildDoc(string uri) { using (XmlReader reader = XmlReader.Create(uri)) { reader.MoveToContent(); // Parse the file and display each of the nodes. while (!reader.EOF) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "Child") { XElement el = XElement.ReadFrom(reader) as XElement; if (el != null) yield return el; } else { reader.Read(); } } } }
Kenny evitt
source share