-3

I'm trying to read an xml and display its data in a form. Following is the code generated with the form. If I want to just display "Hello World" from a tab value in XML, where should I put the code in the C# code generated by the VB form, if my Xml schema (test.xml)is as below.

   <tab>
        <message>Hello World</Message>
   </tab>

Following is the code generated by the form. I included System.Xml to read the Xml file. Any help is greatly appreciated. Thank you

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace xmldatatest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\test.xml");
        }
     }
 }
  • 2
    *VB C#* is not a language, and we're not a code writing service. What code have you written so far to attempt to do this yourself? What specific problem are you having with that code? Please [edit] your question to include the code you've tried, explain how it doesn't work as it should, and ask a specific question related to that code, and we'll be happy to try and help. – Ken White Oct 27 '16 at 03:23
  • [so] have a lot of resources to help you read the XML, like this (http://stackoverflow.com/questions/28656686/how-to-read-xml-data-using-c-sharp?rq=1) and this (http://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c?rq=1) – Prisoner Oct 27 '16 at 03:56
  • @Ken White: My apologies. I changed my question and the simple code I have. – shapedworld Oct 27 '16 at 04:02
  • @ Alex: Thank you. Resource links you provided answered my question. – shapedworld Oct 27 '16 at 04:03

2 Answers2

1

Correct your XML File to be valid:

<?xml version="1.0" encoding="utf-8" ?>
<tab>
    <message>Hello World</message>
</tab>

C# code should be like:

public partial class Form1 : Form
{
    public Form1()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("C:\\test.xml");

        var node = doc.SelectSingleNode("/tab/message");

        // Gets "Hello World"
        var message = node.InnerText;

        // you can do whatever with the message now...
    }
}
Daniel
  • 9,491
  • 12
  • 50
  • 66
0

The XML example you supplied is not valid:

<tab>
     <message>Hello World</Message>
</tab>

Your message tag starts with this <message> but ends with this </Message>.

jussij
  • 10,370
  • 1
  • 33
  • 49
  • Hi Jussij, I only added schema but didn't included the part in my question. to keep the question short. I'll include it next time. Thanks – shapedworld Oct 27 '16 at 23:17