2

Using Jon Skeet's code

static IEnumerable<XElement> SimpleStreamAxis(string inputUrl,
                                              string elementName)
{
  using (XmlReader reader = XmlReader.Create(inputUrl))
  {
    reader.MoveToContent();
    while (reader.Read())
    {
      if (reader.NodeType == XmlNodeType.Element)
      {
        if (reader.Name == elementName)
        {
          XElement el = XNode.ReadFrom(reader) as XElement;
          if (el != null)
          {
            yield return el;
          }
        }
      }
    }
  }
}

He posted on this thread: Jon Skeet's solution

After I read the data, I use File.Move(currentDest,newDest) and I keep getting that the file is currently in use by another process. I have checked to see that there is no editor that has the file open or even browser. In addition I get part of the path could not be found

Does the yield within the using not close the reader ? do i have to close it explicitly?

File Move code

File.Move(Path.Combine(InPath, "test.xml"), Path.Combine(OutPath, "test.xml"));

EDIT: It looks like the yield within the using streamreader does not close the file. Can anyone confirm this?

Community
  • 1
  • 1
TCSCFA
  • 21
  • 3
  • `using` should invoke Dispose(), which should close the stream... – Eric J. Aug 12 '11 at 18:27
  • Have you tried a pure Linq to Xml solution or is your xml file too large to use the DOM approach? – Billy Aug 12 '11 at 18:36
  • 2
    You should post a answer to your own question so it wont keep showing up on the front page as unanswered. – Scott Chamberlain Aug 12 '11 at 18:51
  • @Scott, I do not qualify to answer my own ? – TCSCFA Aug 12 '11 at 19:04
  • I have answered my own question before. You found what was wrong and as long as you write it up as an answer then yes:-) It also stops others from opening the post and reading everything just to find you solved it(as part of the question). – Billy Aug 12 '11 at 19:16
  • @all: what i thought was the solution isn't. the yield statement used above does not close the file as file.move operation now reports that the file is in use by another process. – TCSCFA Aug 12 '11 at 19:30
  • That's how yield works...see SO question referenced in my answer update. – Josh Anderson Aug 12 '11 at 20:06

2 Answers2

1

Have you tried explicitly closing the reader before exiting the using block?

Update
When you use yield, it doesn't actually do anything until you enumerate the IEnumerable. This is why you're having issues with the file staying open.

See this SO question: yield return statement inside a using() { } block Disposes before executing

You're probably going to have to create an IEnumerable, populate it, close your stream, then return the IEnumerable.

Community
  • 1
  • 1
Josh Anderson
  • 5,975
  • 2
  • 35
  • 48
0

If you can use a pure Linq to Xml solution you can do:

static IEnumerable<XElement> SimpleStreamAxis(string filePathAndName, string elementName)
  {
     XDocument doc = XDocument.Load(filePathAndName);

     IEnumerable<XElement> results = from n in doc.Descendants(elementName) 
                   select n; 
     //Do more stuff with the results as needed...

     doc.Save(filePathAndName); //Note you can change the location by supplying a different path

     return results;
  }
Billy
  • 2,600
  • 6
  • 31
  • 36