Thursday, October 11, 2007

Let's Talk about .. Visual Studio 2005-Get the text from XML

My senior office colleague, P'Tuu, told me about how I can get the text from some tags of the XML file.
if my XML file is


New Year Day


First, I want "New Year Day" which is in the Day tag. I can use the following instructions to get it.

[VB.Net]
If Not (xdoc.SelectSingleNode("/Year/Month/Day") Is Nothing) Then
Session("DATE") = xdoc.SelectSingleNode("/Year/Month/Day").InnerXml
End If

[C#.Net]
if (xdoc.SelectSingleNode("/Year/Month/Day") != null)
{
DATE = xdoc.SelectSingleNode("/Year/Month/Day").InnerXml;
}
Second, I want "123" which is the value of the attribute Level inside Day's tag. I can use this instructions to get it.
[VB.Net]
If Not (xdoc.SelectSingleNode("/Year/Month/Day/@Level") Is Nothing) Then
Session("DATE_Level") = xdoc.SelectSingleNode("/Year/Month/Day/@Level").InnerXml
End If

[C#.Net]
if (xdoc.SelectSingleNode("/Year/Month/Day/@Level") != null)
{
DATE_Level = xdoc.SelectSingleNode("/Year/Month/Day/@Level").InnerXml;
}

No comments: