Get Element Value

Discussion in 'ASP .Net Web Services' started by Jay, May 17, 2006.

  1. Jay

    Jay Guest

    In a loop like so:

    ....
    xmlr=cmd.executexmlreader()
    xmlr.read()
    do while xmlr.readstate <> xml.readstate.endoffile

    loop

    How do I return each individual element name and value from a document like
    so:

    <Customer>
    <custno>2</custno>
    <lname>Doe</lname>
    <fname>John</fname>
    <address>123 4th st.</address>
    </customer>


    So the result I need in the loop is (output to textbox):

    custno: 2
    lname: Doe
    ....


    Thanks a lot.
     
    Jay, May 17, 2006
    #1
    1. Advertising

  2. Jay

    Jay Guest

    Thanks for the reply.

    This seems to work well, however, I am faced with a problem...

    I need to get the etire ReadOuterXML for each of the individual records as
    well as a few of the element values within that record.

    So with XML like so:

    <Customer>
    <custno>2</custno>
    <lname>Doe</lname>
    <fname>John</fname>
    <address>123 4th st.</address>
    </customer>
    <Customer>
    <custno>3</custno>
    <lname>Smith</lname>
    <fname>Terry</fname>
    <address>456 7th St.</address>
    </customer>

    I need to return (in a loop) something like this:

    varOuterXML =
    "<Customer><custno>2</custno><lname>Doe</lname><fname>John</fname><address>123
    4th st.</address></customer>"
    varLName="Doe"
    varFName="John"

    varOuterXML =
    "<Customer><custno>3</custno><lname>Smith</lname><fname>Terry</fname><address>456
    7th St.</address></customer>"
    varLName="Smith"
    varFName="Terry"


    I am using while xmlreader.readstate <> xml.readstate.endoffile but cannot
    seem to get this to work together.

    Any suggestions would be greatly appreciated.

    Thanks a lot.






    "Gaurav Vaish (EduJini.IN)" <> wrote in
    message news:...
    > while(reader.Read())
    > {
    > if(reader.NodeType == XmlNodeType.Element)
    > {
    > _name = reader.Name;
    > if(reader.read())
    > {
    > if(reader.NodeType == XmlNodeType.Text)
    > {
    > _value = reader.Value;
    > }
    > }
    > }
    > }
    >
    > or
    >
    > string _name;
    > string _value;
    >
    > while(r.Read())
    > {
    > switch(reader.NodeType)
    > {
    > XmlNodeType.Element: _name = reader.Name;
    > break;
    > XmlNodeType.Text: if(_name != null)
    > {
    > _value = reader.Value;
    > _name = null;
    > } // Reset the _name after the value
    > has been found
    > }
    > }
    >
    >
    > HTH
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish
    > http://www.mastergaurav.org
    > http://www.edujini.in
    > -------------------
    >
    >
    > "Jay" <msnews.microsoft.com> wrote in message
    > news:...
    >> In a loop like so:
    >>
    >> ...
    >> xmlr=cmd.executexmlreader()
    >> xmlr.read()
    >> do while xmlr.readstate <> xml.readstate.endoffile
    >>
    >> loop
    >>
    >> How do I return each individual element name and value from a document
    >> like so:
    >>
    >> <Customer>
    >> <custno>2</custno>
    >> <lname>Doe</lname>
    >> <fname>John</fname>
    >> <address>123 4th st.</address>
    >> </customer>
    >>
    >>
    >> So the result I need in the loop is (output to textbox):
    >>
    >> custno: 2
    >> lname: Doe
    >> ...
    >>
    >>
    >> Thanks a lot.
    >>
    >>
    >>
    >>
    >>

    >
    >
     
    Jay, May 18, 2006
    #2
    1. Advertising

  3. Jay

    Jay Guest

    Thank you for the reply. Not quite sure what you mean.

    I need both the entire outerxml as well as a few individual element values.
    Would I use a loop inside the loop?

    "Gaurav Vaish (EduJini.IN)" <> wrote in
    message news:...
    > Don't use single _name and _value.
    > When you encounter an element with the name Customer, jump into another
    > function where you keep track of all entries.
    >
    > Should be simple.
    >
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish
    > http://www.mastergaurav.org
    > http://www.edujini.in
    > -------------------
    >
    >
    > "Jay" <msnews.microsoft.com> wrote in message
    > news:...
    >> Thanks for the reply.
    >>
    >> This seems to work well, however, I am faced with a problem...
    >>
    >> I need to get the etire ReadOuterXML for each of the individual records
    >> as well as a few of the element values within that record.
    >>
    >> So with XML like so:
    >>
    >> <Customer>
    >> <custno>2</custno>
    >> <lname>Doe</lname>
    >> <fname>John</fname>
    >> <address>123 4th st.</address>
    >> </customer>
    >> <Customer>
    >> <custno>3</custno>
    >> <lname>Smith</lname>
    >> <fname>Terry</fname>
    >> <address>456 7th St.</address>
    >> </customer>
    >>
    >> I need to return (in a loop) something like this:
    >>
    >> varOuterXML =
    >> "<Customer><custno>2</custno><lname>Doe</lname><fname>John</fname><address>123
    >> 4th st.</address></customer>"
    >> varLName="Doe"
    >> varFName="John"
    >>
    >> varOuterXML =
    >> "<Customer><custno>3</custno><lname>Smith</lname><fname>Terry</fname><address>456
    >> 7th St.</address></customer>"
    >> varLName="Smith"
    >> varFName="Terry"
    >>
    >>
    >> I am using while xmlreader.readstate <> xml.readstate.endoffile but
    >> cannot seem to get this to work together.
    >>
    >> Any suggestions would be greatly appreciated.
    >>
    >> Thanks a lot.
    >>
    >>
    >>
    >>
    >>
    >>
    >> "Gaurav Vaish (EduJini.IN)" <> wrote
    >> in message news:...
    >>> while(reader.Read())
    >>> {
    >>> if(reader.NodeType == XmlNodeType.Element)
    >>> {
    >>> _name = reader.Name;
    >>> if(reader.read())
    >>> {
    >>> if(reader.NodeType == XmlNodeType.Text)
    >>> {
    >>> _value = reader.Value;
    >>> }
    >>> }
    >>> }
    >>> }
    >>>
    >>> or
    >>>
    >>> string _name;
    >>> string _value;
    >>>
    >>> while(r.Read())
    >>> {
    >>> switch(reader.NodeType)
    >>> {
    >>> XmlNodeType.Element: _name = reader.Name;
    >>> break;
    >>> XmlNodeType.Text: if(_name != null)
    >>> {
    >>> _value = reader.Value;
    >>> _name = null;
    >>> } // Reset the _name after the value
    >>> has been found
    >>> }
    >>> }
    >>>
    >>>
    >>> HTH
    >>>
    >>> --
    >>> Happy Hacking,
    >>> Gaurav Vaish
    >>> http://www.mastergaurav.org
    >>> http://www.edujini.in
    >>> -------------------
    >>>
    >>>
    >>> "Jay" <msnews.microsoft.com> wrote in message
    >>> news:...
    >>>> In a loop like so:
    >>>>
    >>>> ...
    >>>> xmlr=cmd.executexmlreader()
    >>>> xmlr.read()
    >>>> do while xmlr.readstate <> xml.readstate.endoffile
    >>>>
    >>>> loop
    >>>>
    >>>> How do I return each individual element name and value from a document
    >>>> like so:
    >>>>
    >>>> <Customer>
    >>>> <custno>2</custno>
    >>>> <lname>Doe</lname>
    >>>> <fname>John</fname>
    >>>> <address>123 4th st.</address>
    >>>> </customer>
    >>>>
    >>>>
    >>>> So the result I need in the loop is (output to textbox):
    >>>>
    >>>> custno: 2
    >>>> lname: Doe
    >>>> ...
    >>>>
    >>>>
    >>>> Thanks a lot.
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Jay, May 18, 2006
    #3
  4. Jay

    Guest Guest

    The XML I'm using comes from SQL Server 2005 and isn't very large at all. I
    am having a difficult time with the loop used to capture the individual
    element values as well as the outerxml. Would you happen to know of an
    example illustrating this or tutorials on accomplishing this? Thank you
    very much.


    "Gaurav Vaish (EduJini.IN)" <> wrote in
    message news:uSMt$...
    > You need to keep a track of all the values in a buffer.
    > At the same time, you need to keep storing the individual values.
    >
    > Similar to what the XmlDocument does.
    >
    > btw, just wondering, why don't you directly use XmlDocument? If the XML
    > file a huge one?
    >
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish
    > http://www.mastergaurav.org
    > http://www.edujini.in
    > -------------------
    >
    >
    > "Jay" <msnews.microsoft.com> wrote in message
    > news:%...
    >> Thank you for the reply. Not quite sure what you mean.
    >>
    >> I need both the entire outerxml as well as a few individual element
    >> values. Would I use a loop inside the loop?
    >>
    >> "Gaurav Vaish (EduJini.IN)" <> wrote
    >> in message news:...
    >>> Don't use single _name and _value.
    >>> When you encounter an element with the name Customer, jump into another
    >>> function where you keep track of all entries.
    >>>
    >>> Should be simple.
    >>>
    >>>
    >>> --
    >>> Happy Hacking,
    >>> Gaurav Vaish
    >>> http://www.mastergaurav.org
    >>> http://www.edujini.in
    >>> -------------------
    >>>
    >>>
    >>> "Jay" <msnews.microsoft.com> wrote in message
    >>> news:...
    >>>> Thanks for the reply.
    >>>>
    >>>> This seems to work well, however, I am faced with a problem...
    >>>>
    >>>> I need to get the etire ReadOuterXML for each of the individual records
    >>>> as well as a few of the element values within that record.
    >>>>
    >>>> So with XML like so:
    >>>>
    >>>> <Customer>
    >>>> <custno>2</custno>
    >>>> <lname>Doe</lname>
    >>>> <fname>John</fname>
    >>>> <address>123 4th st.</address>
    >>>> </customer>
    >>>> <Customer>
    >>>> <custno>3</custno>
    >>>> <lname>Smith</lname>
    >>>> <fname>Terry</fname>
    >>>> <address>456 7th St.</address>
    >>>> </customer>
    >>>>
    >>>> I need to return (in a loop) something like this:
    >>>>
    >>>> varOuterXML =
    >>>> "<Customer><custno>2</custno><lname>Doe</lname><fname>John</fname><address>123
    >>>> 4th st.</address></customer>"
    >>>> varLName="Doe"
    >>>> varFName="John"
    >>>>
    >>>> varOuterXML =
    >>>> "<Customer><custno>3</custno><lname>Smith</lname><fname>Terry</fname><address>456
    >>>> 7th St.</address></customer>"
    >>>> varLName="Smith"
    >>>> varFName="Terry"
    >>>>
    >>>>
    >>>> I am using while xmlreader.readstate <> xml.readstate.endoffile but
    >>>> cannot seem to get this to work together.
    >>>>
    >>>> Any suggestions would be greatly appreciated.
    >>>>
    >>>> Thanks a lot.
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>>
    >>>> "Gaurav Vaish (EduJini.IN)" <>
    >>>> wrote in message news:...
    >>>>> while(reader.Read())
    >>>>> {
    >>>>> if(reader.NodeType == XmlNodeType.Element)
    >>>>> {
    >>>>> _name = reader.Name;
    >>>>> if(reader.read())
    >>>>> {
    >>>>> if(reader.NodeType == XmlNodeType.Text)
    >>>>> {
    >>>>> _value = reader.Value;
    >>>>> }
    >>>>> }
    >>>>> }
    >>>>> }
    >>>>>
    >>>>> or
    >>>>>
    >>>>> string _name;
    >>>>> string _value;
    >>>>>
    >>>>> while(r.Read())
    >>>>> {
    >>>>> switch(reader.NodeType)
    >>>>> {
    >>>>> XmlNodeType.Element: _name = reader.Name;
    >>>>> break;
    >>>>> XmlNodeType.Text: if(_name != null)
    >>>>> {
    >>>>> _value = reader.Value;
    >>>>> _name = null;
    >>>>> } // Reset the _name after the
    >>>>> value has been found
    >>>>> }
    >>>>> }
    >>>>>
    >>>>>
    >>>>> HTH
    >>>>>
    >>>>> --
    >>>>> Happy Hacking,
    >>>>> Gaurav Vaish
    >>>>> http://www.mastergaurav.org
    >>>>> http://www.edujini.in
    >>>>> -------------------
    >>>>>
    >>>>>
    >>>>> "Jay" <msnews.microsoft.com> wrote in message
    >>>>> news:...
    >>>>>> In a loop like so:
    >>>>>>
    >>>>>> ...
    >>>>>> xmlr=cmd.executexmlreader()
    >>>>>> xmlr.read()
    >>>>>> do while xmlr.readstate <> xml.readstate.endoffile
    >>>>>>
    >>>>>> loop
    >>>>>>
    >>>>>> How do I return each individual element name and value from a
    >>>>>> document like so:
    >>>>>>
    >>>>>> <Customer>
    >>>>>> <custno>2</custno>
    >>>>>> <lname>Doe</lname>
    >>>>>> <fname>John</fname>
    >>>>>> <address>123 4th st.</address>
    >>>>>> </customer>
    >>>>>>
    >>>>>>
    >>>>>> So the result I need in the loop is (output to textbox):
    >>>>>>
    >>>>>> custno: 2
    >>>>>> lname: Doe
    >>>>>> ...
    >>>>>>
    >>>>>>
    >>>>>> Thanks a lot.
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Guest, May 19, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. x
    Replies:
    3
    Views:
    886
  2. Matthet
    Replies:
    2
    Views:
    711
    Matthet
    Sep 24, 2003
  3. HANM
    Replies:
    2
    Views:
    764
    Joseph Kesselman
    Jan 29, 2008
  4. PeteOlcott
    Replies:
    1
    Views:
    974
    Mukul Gandhi
    Feb 5, 2009
  5. OccasionalFlyer
    Replies:
    6
    Views:
    264
    Garrett Smith
    Jul 29, 2009
Loading...

Share This Page