Kevin Vogler wrote:
I'm still feeling my way around XML parsing and have an app that uses XML
reader and works fine as long as the XML is pretty but doesn't find all the
elements when the XML is all on a single line without the carriage returns
after each element. As I understand it XMLReader uses the carriage return as
a stop for each read.
Before I start to rewrite this, I would like to know if there's a way to
deal with this with XMLReader or will using xpath or LINQ avoid this issue?
XPath or LINQ to XML work on an in-memory tree model of the XML document
while XmlReader is a low-level forwards only pull parsing approach. So
you can certainly avoid some of the hassles of using XmlReader by
switching to XPath or LINQ to XML.
On the other hand it is certainly possible to process XML with XmlReader
even if it is not indented, here is an example where the same XmlReader
logic is used to process different XML strings:
foreach (string xml in new string[] { @"<root>
<foo>foo 1</foo>
</root>",
@"<root><foo>fo o 2</foo></root>" })
{
using (XmlReader reader = XmlReader.Creat e(new
StringReader(xm l)))
{
while (reader.Read())
{
if (reader.NodeTyp e == XmlNodeType.Ele ment &&
reader.LocalNam e == "foo")
{
Console.WriteLi ne("foo: {0}",
reader.ReadStri ng());
}
}
}
}
If you need further help then show us the XML you have and explain which
data you want to extract, then it is possible to help writing XmlReader
code for that.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/