XML parsing

studio72

Member
Joined
Sep 26, 2010
Messages
6
Programming Experience
Beginner
Hi,

first I want to excuse me if I post in wrong section.
I need some help, I don't have experience in parsing XML's.
I have this type of XML
HTML:
	<MainInfo>
		<info name="ABCD"/>
		<InfoDetails>
			<Detail Attribute="age" Value="12" />
			<Detail Attribute="gender" Value="male" />
			<Detail Attribute="country" Value="england" />
			<Detail Attribute="Comment" Value="smart and hard worker" />
		</InfoDetails>
	</MainInfo>
I want to know how to extract in one string the value from attribute "Comment", the "smart and hard worker", I work in VB.NET
I want in this way, DIM xmlvalue AS STRING and in this variable to put "smart and hard worker"

I tried something in this way but for sure I'm not doing what must to do :)

           Dim myXMLString As String = "	<MainInfo>
		                                             <info name="ABCD"/>
		                                             <InfoDetails>
			                                           <Detail Attribute="age" Value="12" />
			                                           <Detail Attribute="gender" Value="male" />
			                                           <Detail Attribute="country" Value="england" />
			                                           <Detail Attribute="Comment" Value="smart and hard worker" />
		                                            </InfoDetails>
	                                               </MainInfo>"

            Dim doc As New XmlDocument()
            doc.LoadXml(myXMLString)

            Dim xmlvalue As String

            Dim items = doc.GetElementsByTagName("InfoDetails")

             xmlvalue  = ?



Thanks in advance
 
Last edited by a moderator:
Select @Value attribute from a Detail node where @Attribute = 'Comment'.

Xpath and SelectSingleNode:
Dim xmlvalue = doc.SelectSingleNode("//Detail[@Attribute='Comment']/@Value").Value

XPath Tutorial

I would rather use the newer XDocument class and Linq to Xml:
Dim doc = XDocument.Parse(myXMLString)
Dim xmlvalue = (From node In doc...<Detail> Where node.@Attribute = "Comment" Select node.@Value).First
 
Select @Value attribute from a Detail node where @Attribute = 'Comment'.

Xpath and SelectSingleNode:
Dim xmlvalue = doc.SelectSingleNode("//Detail[@Attribute='Comment']/@Value").Value

XPath Tutorial

I would rather use the newer XDocument class and Linq to Xml:
Dim doc = XDocument.Parse(myXMLString)
Dim xmlvalue = (From node In doc...<Detail> Where node.@Attribute = "Comment" Select node.@Value).First

Thank you very much, it's work

Thanks again
 
Back
Top