last day (14 days later) » 

12:41 PM
1
A: Retrieve first element value using XMLReader C#

JNYRangerSince you are familar with XmlDocument you should just use the XmlReader to load an XmlDocument This can be done easily (assume the XmlReader is named reader): XmlDocument doc = new XmlDocument; doc.Load(reader); //perform queries The only catch with this is that you need to make sure that th...

 
Thanks for your response. XmlReader doc = XmlReader.Create(sr, settings); while (doc.Read()) { if ((doc.NodeType == XmlNodeType.Element) && (doc.Name == "title")) { } Now the problem is, if i write, doc.ReadInnerXML(), it retrives all the text of all the title elements but i need only the first one. Thats where i was stuck
 
One step at a time. First handle the reading. Don't bother with any writes until you have the correct node. My example is to show you how use your XmlReader in an XmlDocument since you said you were already familiar with that. This comment is stating is a completely different question than your original post.
Additionally the loop in your example in your comment should break as soon as you enter the if statement's code block the first time otherwise you'll continue to read the document and enter the conditional code block.
 
Sorry. I am confused. I updated the code now. My requirement is i need to add a block of code to retrieve the first occurrence of the title tag with the XMLreader code itself. I dont want to load the XMLreader into XML doc. I want to continue in this way and do it.
 
I'll edit now that you did. Hold on.
@ShahulHameed Take a look now.
 
I know about the loop but my concern is regarding this line (//do whatever you need to do here w/ this node). Like you have mentioned, i need to one operation and i dont know how. That is my concern. I need to retrieve the value of first title tag. so when say doc.innerxml, it retireves all the values. how can i retrieve only the first value. thats what my concern. Thanks for your response
 
12:47 PM
I moved this to chat because the question in your comment is not the original question. You were asking how to get that element, which my answer shows. In order to handle the element, that's a whole 'nother case. Have you read the XmlReader's documentation yet? msdn.microsoft.com/en-us/library/…
 
 
2 hours later…
2:59 PM
Shahul, you there?
 
hello ranger,
did u get my last post?
it is not getting delivered
 
Yes, that's why I started this chat since it's a different question that your original post
 
No here. in this chatroom
 
ah
No, I did not. Mind resending, I'll try my best to help you out
 
i guess, it is not delivered. Ok let me tell you the problem. But before that, many thanks. thanks for patience and your time
i know how to get the element. so after the loop. i need to do some operation on the element. It generally takes the value of all the title elements in the xml if i try with doc.Readinnerxml()
 
3:05 PM
My pleasure. I like to help out when I can
 
but i need to take one only value out of it. The first title element value.
 
Ok, so what do you want to get out of the element? The value between the tags, or the value of an attribute?
 
not attribute
 
so the value between the tags
 
<title>the doc</title>
yes
 
3:06 PM
like <test>Value Here<?test
gotcha
ok
 
but i have multiple occurrence.
 
XmlReader allows for you to pull the data in a bunch of different ways
that's fine
 
i need to choose the first title element
 
Is the value just a string, or will it be casted to something?
 
hm. i m just learning. THis is my first try :)
string
 
3:07 PM
ok hold on
 
thanks
sure
 
you picked a doozy, XmlReader is the most difficult of the XML processing classes within C#
 
ohh.this is my first try, so dont have any knowledge on different libraries.
 
Ok, so take a look at my code in my answer, and see where I have //do stuff with the node here?
 
so picked one and continued with it, Later came to know about XMLdocument
yeah sure. ll look at it
 
3:09 PM
Yeah, XmlReader should only be used if you need to stream HUGE xml files that cannot be stored in memory. You can only read forward too, so no queries
XDocument is the best of the libraries, XmlDocument is the old style
and XmlReader is for loading into the other two or used directly when you need to stream a file
 
I will try to use Xdocument hereafter. Thanks for the suggestion
 
So where I wrote //Do stuff w/ the node here you would then call this:
 
i m not getting it
what did u say?
 
string title = newdoc.ReadElementContentAsString()
 
ok..
as now, it will store all the values. Didnt it?
 
3:11 PM
Well it depends
If you use the construct that I described in my answer, then it would only get the first
Remember that XmlReader reads forward only, and that flag in my answer will prevent it from doing it more than once
 
oh. ok i will try it out now. thanks a lot for your help
 
No problem. Good luck!
 

last day (14 days later) »