473,378 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

XML without carriage returns after nodes

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
Jul 28 '08 #1
4 3367
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>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/
Jul 28 '08 #2
Kevin Vogler wrote:
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.
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/
Jul 28 '08 #3
Kevin Vogler wrote:
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/
Jul 28 '08 #4
Martin,

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

Thanks,
Kevin Vogler
Jul 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yawnmoth | last post by:
so... i'm trying to remove all carriage returns in the input i get from GET, and am trying to replace them with three dots... however, this never seems to work... i'm still getting carriage...
4
by: Les Juby | last post by:
Can someone please help with a suggestion as to how I can keep the formatting (carriage returns) that the user enters into a memo field and then display that later. I figured I might be able to...
2
by: Andrew Chanter | last post by:
I have a VBA function that returns a string including "vbcr" (VB Carriage Return) to seperate a list into multiple rows, eg Item1 & vbcr & Item2 & vbcr & Item3 This works as planned in the...
12
by: Nimmy | last post by:
Hi, I have a data file and I want to remove Carriage returns. Any one has any C code/program which does this? I am working on Windows XP machine.....I don't have access to UNIX machine. But I...
7
by: mattrapoport | last post by:
I have a page with a div on it. The div displays a user comment. When the user logs into this page, their current comment is pulled from a db and displayed in the div. The user can edit the...
6
by: shajias | last post by:
Hi, I am having a xml code like this <name> I am having a carriage return here. Second line with carriage return. This is the last line. </name>
2
by: GregBeagle | last post by:
Windows XP I have a simple form from which I want to generate an email with the contents of the form. I use carriage returns to format the content for readability. When I test it on my computer...
0
by: markus.shure | last post by:
Hi, I'm noticed a problem testing a JAX-WS client with a WSE server. The JAX-WS client adds carriage returns to a SOAP header element that is signed. This causes the WSE server to raise an...
11
by: evenlater | last post by:
My db allows the user to send email via CDO. The body of the email is determined in code. I have built an email form with To, CC and Subject lines and a large text box for the body of the message...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.