XML without carriage returns after nodes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Vogler

    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


  • Martin Honnen

    #2
    Re: XML without carriage returns after nodes

    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

    Comment

    • Martin Honnen

      #3
      Re: XML without carriage returns after nodes

      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

      Comment

      • Martin Honnen

        #4
        Re: XML without carriage returns after nodes

        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.LocalNam e
        If reader.NodeType = XmlNodeType.Ele ment Then
        Select Case LName
        Case "SERVICE"
        SERVICE =
        reader.ReadElem entContentAsStr ing
        Case "CARRIERACCOUNT "
        CARRIERACCOUNT =
        reader.ReadElem entContentAsStr ing
        End Select
        End If
        End While
        Use ReadString() instead of ReadElementCont entAsString(). The problem
        with your code above is that ReadElementCont entAsString() 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

        Comment

        • Kevin Vogler

          #5
          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


          Comment

          Working...