Get Element Value

  • Thread starter Thread starter Jay
  • Start date Start date
J

Jay

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.
 
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
 
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
 
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.
 
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.
 
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?
 
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?
 
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.
 
I'd suggest using XmlDocument in that case.

Examples... I'd not be aware. I'm a nerd ;-)
 
Back
Top