-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");
        }
     }
 }
4
  • 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. Commented Oct 27, 2016 at 3:23
  • Stack Overflow have a lot of resources to help you read the XML, like this (stackoverflow.com/questions/28656686/…) and this (stackoverflow.com/questions/642293/…) Commented Oct 27, 2016 at 3:56
  • @Ken White: My apologies. I changed my question and the simple code I have. Commented Oct 27, 2016 at 4:02
  • @ Alex: Thank you. Resource links you provided answered my question. Commented Oct 27, 2016 at 4:03

2 Answers 2

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...
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Daniel, I got the program to out put the text stored in "message" variable. Thank you
@shapedworld glad to hear that you got it working. If this is the answer then please mark it as answer. Thx.
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>.

1 Comment

Hi Jussij, I only added schema but didn't included the <?xml version="1.0" encoding="utf-8" ?> part in my question. to keep the question short. I'll include it next time. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.