Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2008

Tip: Looking for answers? Try searching our database.

XML without carriage returns after nodes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kevin Vogler - 28 Jul 2008 14:09 GMT
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 - 28 Jul 2008 14:22 GMT
> 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
[quoted text clipped - 4 lines]
> 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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Kevin Vogler - 28 Jul 2008 15:07 GMT
Martin,
Thanks for your prompt response. I was already using the method
(reader.NodeType == XmlNodeType.Element &&
> reader.LocalName == "foo")  you show in your code to locate the elements I
> want. But I'm confused on the xml you use in the example. Are the line
> feeds in the xml intentional. The sample works as you presented it but if
> edit (by removing the line feeds) your foo xml to resemble the file I'm
> receiving, it errors out.

Here's a sample of the file I'm receiving:

<?xml
version="1.0"?><ORDER><HEADER><PURCHASEORDERNUMBER>000042PAI20080725T113844420</PURCHASEORDERNUMBER><MERCHANTNUMBER>463565014444
</MERCHANTNUMBER><DBAINFORMATION><NAME>ELITE SIGNS AND
GRAPHICS</NAME><ATTN>JEFF
</ATTN><STREETADDRESS1>8628 UTICA AVE STE
600</STREETADDRESS1><STREETADDRESS2/><CITY>RANCHO CUCAMONGA
</CITY><STATE>CA</STATE><ZIP>91730</ZIP><PHONENUMBER>9099898581</PHONENUMBER></DBAINFORMATION><SHIPPING><ADDRESS><NAME>ELITE
SIGNS AND GRAPHICS</NAME><ATTN>JEFF
</ATTN><STREETADDRESS1>8628 UTICA AVE STE
600</STREETADDRESS1><STREETADDRESS2/><CITY>RANCHO CUCAMONGA
</CITY><STATE>CA</STATE><ZIP>91730</ZIP><PHONENUMBER>9099898581</PHONENUMBER></ADDRESS><CARRIER><NAME>FedEx</NAME><SERVICE>Standard
Overnight</SERVICE><CARRIERACCOUNT>FEDEX-ACCOUNT-NUMBER</CARRIERACCOUNT></CARRIER></SHIPPING></HEADER><DETAIL><ORDERGROUP><PURCHASE><PRODUCT><PRODUCTID>1929</PRODUCTID><DESCRIPTION>XL
300</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>TERMINAL</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1962</PRODUCTID><DESCRIPTION>Imprinter</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>IMPRINT</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1963</PRODUCTID><DESCRIPTION>Imprinter
Plate</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>IMPRINT</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1961</PRODUCTID><DESCRIPTION>Welcome
Kit</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>KITS</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1931</PRODUCTID><DESCRIPTION>PIN
Pad
292</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>PINPAD</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><IMPRINTERPLATEINFO><PLATELINE1>line
1</PLATELINE1><PLATELINE2>line 2</PLATELINE2><PLATELINE3 /><PLATELINE4
/></IMPRINTERPLATEINFO><INDUSTRYTYPE>Retail</INDUSTRYTYPE></PURCHASE></ORDERGROUP></DETAIL></ORDER>

There are no line feeds in the file. Is this typical? Can I work with this?
If not will the other methods parse this?

Many thanks,
Kevin Vogler
Martin Honnen - 28 Jul 2008 15:42 GMT
> 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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Kevin Vogler - 28 Jul 2008 16:25 GMT
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

My XML example:
<?xml
version="1.0"?><ORDER><HEADER><PURCHASEORDERNUMBER>000042PAI20080725T113844420</PURCHASEORDERNUMBER><MERCHANTNUMBER>463565014444
</MERCHANTNUMBER><DBAINFORMATION><NAME>ELITE SIGNS AND
GRAPHICS</NAME><ATTN>JEFF
</ATTN><STREETADDRESS1>8628 UTICA AVE STE
600</STREETADDRESS1><STREETADDRESS2/><CITY>RANCHO CUCAMONGA
</CITY><STATE>CA</STATE><ZIP>91730</ZIP><PHONENUMBER>9099898581</PHONENUMBER></DBAINFORMATION><SHIPPING><ADDRESS><NAME>ELITE
SIGNS AND GRAPHICS</NAME><ATTN>JEFF
</ATTN><STREETADDRESS1>8628 UTICA AVE STE
600</STREETADDRESS1><STREETADDRESS2/><CITY>RANCHO CUCAMONGA
</CITY><STATE>CA</STATE><ZIP>91730</ZIP><PHONENUMBER>9099898581</PHONENUMBER></ADDRESS><CARRIER><NAME>FedEx</NAME><SERVICE>Standard
Overnight</SERVICE><CARRIERACCOUNT>FEDEX-ACCOUNT-NUMBER</CARRIERACCOUNT></CARRIER></SHIPPING></HEADER><DETAIL><ORDERGROUP><PURCHASE><PRODUCT><PRODUCTID>1929</PRODUCTID><DESCRIPTION>XL
300</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>TERMINAL</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1962</PRODUCTID><DESCRIPTION>Imprinter</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>IMPRINT</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1963</PRODUCTID><DESCRIPTION>Imprinter
Plate</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>IMPRINT</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1961</PRODUCTID><DESCRIPTION>Welcome
Kit</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>KITS</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><PRODUCT><PRODUCTID>1931</PRODUCTID><DESCRIPTION>PIN
Pad
292</DESCRIPTION><NEWREFURB>New</NEWREFURB><PRODUCTTYPE>PINPAD</PRODUCTTYPE><NOTES/><QUANTITY>1</QUANTITY><TID/><DOWNLOADSERIALNUMBER/><DOWNLOADPHONE/></PRODUCT><IMPRINTERPLATEINFO><PLATELINE1>line
1</PLATELINE1><PLATELINE2>line 2</PLATELINE2><PLATELINE3 /><PLATELINE4
/></IMPRINTERPLATEINFO><INDUSTRYTYPE>Retail</INDUSTRYTYPE></PURCHASE></ORDERGROUP></DETAIL></ORDER>
Martin Honnen - 28 Jul 2008 16:39 GMT
> Thanks for the response.
>
[quoted text clipped - 21 lines]
>                         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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Kevin Vogler - 28 Jul 2008 16:52 GMT
Martin,

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

Thanks,
Kevin Vogler

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.