Hi I have what I think is a simple problem but after much searching cant seem to find a solution. I have an xml file stored on an web server and I need to read the data using vb.net for a smart device application.

I also need the ability to select indvidual nodes of the xml file. It currently a simple file but will be expanded upon.

<?xml version="1.0" encoding="utf-8"?>
<pupil>
	<name>Test Name</name>
	<tagid>00000000000000000001</tagid>
</pupil>

I need to access the name node to start with.

Any Help and Advice Appreiated

Recommended Answers

All 3 Replies

>I also need the ability to select indvidual nodes of the xml file.

Use DOM API (System.XML namespace)

Dim doc As New System.Xml.XmlDocument
        doc.Load("filename.xml")
        Dim list = doc.GetElementsByTagName("name")

        For Each item As System.Xml.XmlElement In list
            Console.WriteLine(item.InnerText)
        Next

Here is a method to read it using LINQ to XML (System.Xml.Linq). This will create an IEnumerable(of T), where T is an anonymous type.

Sub Main()

        Dim xml As String = "<?xml version=""1.0"" encoding=""utf-8""?>" _
                            & "<pupil>" _
                              & "<name>Test Name</name>" _
                             & "<tagid>00000000000000000001</tagid>" _
                            & "</pupil>"

        Dim document As XDocument = XDocument.Parse(xml)

        Dim pupils = From pupil In document.Descendants("pupil") _
                     Select New With _
                     { _
                        .Name = pupil.Element("name").Value, _
                        .TagID = pupil.Element("tagid").Value _
                     }

        For Each pupil In pupils
            Console.WriteLine("{0}: {1}", pupil.Name, pupil.TagID)
        Next

        Console.Read()

    End Sub
static void Main(string[] args)
        {
            string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
                            <pupil>
	                            <name>Test Name</name>
	                            <tagid>00000000000000000001</tagid>
                            </pupil>";

            XDocument document = XDocument.Parse(xml);

            var pupils = from pupil in document.Descendants("pupil")
                         select new
                         {
                             Name = pupil.Element("name").Value,
                             TagID = pupil.Element("tagid").Value
                         };

            foreach (var pupil in pupils)
                Console.WriteLine("{0}: {1}", pupil.Name, pupil.TagID);

            Console.Read();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.