0

Due to memory requirements I have to use XmlReader to process xml files, so I am new to this class. Before I bake something unusual, how to perform a nested read using XmlReader?

What I mean, I am in the middle of xml file, at some node <p> -- I would like to read no further than to closing </p> to this very <p> I mentioned.

This "nested read" (i.e. read which does not go any further) is useful when using recursive call to some processing functions. Normal Read would continue up to end of the file.

3
  • 1
    Have you seen the ReadSubtree method, or are you looking for something else? Commented Feb 18, 2015 at 10:12
  • @O.R.Mapper too many Read... ;-) I missed it, thank you very much, could you post your comment as an answer (so I could accept it). Commented Feb 18, 2015 at 10:14
  • Done, with a little extra information. Commented Feb 18, 2015 at 10:18

1 Answer 1

2

It sounds like you are looking for the ReadSubtree method - that method treats the current element like a document of its own, for which an XmlReader is returned. After closing that reader, your original reader will be placed after the closing tag of the read element.

One remark about your question:

Normal Read would continue up to end of the file.

Well, not automatically so, but you're right that one needs to take some care not to overshoot the closing tag, and there is no way to ensure that other components will not do so when passing the original reader.

And one caveat when using ReadSubtree: The new reader will not point to the opening tag of the element initially, it will point to the position right before the opening tag. Therefore, after invoking ReadSubtree, you normally first want to call Read() once on the subtree reader.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you once again for added details!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.