Connecting Tech Pros Worldwide Forums | Help | Site Map

XML without carriage returns after nodes

Kevin Vogler
Guest
 
Posts: n/a
#1: Jul 28 '08
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?

Many thanks in advance for your assistance,
Kevin



Martin Honnen
Guest
 
Posts: n/a
#2: Jul 28 '08

re: XML without carriage returns after nodes


Kevin Vogler wrote:
Quote:
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>foo 2</foo></root>" })
{
using (XmlReader reader = XmlReader.Create(new
StringReader(xml)))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element &&
reader.LocalName == "foo")
{
Console.WriteLine("foo: {0}",
reader.ReadString());
}
}
}
}


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/
Martin Honnen
Guest
 
Posts: n/a
#3: Jul 28 '08

re: XML without carriage returns after nodes


Kevin Vogler wrote:
Quote:
There are no line feeds in the file. Is this typical? Can I work with this?
Sure, why not? I did already show an example that parsed out some data
from an XML document that had no line feeds.
If you need help parsing out certain data from the XML you posted then
explain which data you are looking for.
Quote:
If not will the other methods parse this?
As said, XPath or LINQ to XML work on tree models so they provide more
power and flexibility than the forwards only XmlReader model. But I am
pretty sure that XmlReader is used under the hood to build those tree
models, there is nothing that prevents XmlReader from parsing a
well-formed XML document with or without line feeds.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Martin Honnen
Guest
 
Posts: n/a
#4: Jul 28 '08

re: XML without carriage returns after nodes


Kevin Vogler wrote:
Quote:
Thanks for the response.
>
In the XML example that i included(see below), for instance there is a
LocalName SERVICE.
When there are no line feeds in the file, my code (below) cannot find this
element but does find the Localname CARRIERACCOUNT. Can you explain where
I've gone wrong?
>
Thanks again for all your help,
Kevin Vogler
>
My code:
While (reader.Read())
Dim LName As String = reader.LocalName
If reader.NodeType = XmlNodeType.Element Then
Select Case LName
Case "SERVICE"
SERVICE =
reader.ReadElementContentAsString
Case "CARRIERACCOUNT"
CARRIERACCOUNT =
reader.ReadElementContentAsString
End Select
End If
End While
Use ReadString() instead of ReadElementContentAsString(). The problem
with your code above is that ReadElementContentAsString() moves the
reader to the next node meaning it is positioned on the next start
element tag, then the While (reader.Read()) consumes that start element
and that way your loop body does never find the element.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Kevin Vogler
Guest
 
Posts: n/a
#5: Jul 28 '08

re: XML without carriage returns after nodes


Martin,

Thank you very much for pointing out my error. This took care of my issue.

Thanks,
Kevin Vogler


Closed Thread