Using form with label and text box controls from XML data

John 466 Reputation points
2023-09-27T00:38:17.6933333+00:00

Now that I have known to populate a Data Grid View control using XML data, how would I use a form

with label and text box controls from XML data? For example, a child node represents a label and a sub-child node represents a text box with data.

An example would be like this XML code snippet.

The EmployeeLastName and EmployeeFirstName would represent the labels, while its data would represent

within the text boxes.

<Employees>
	</Employee>
    	<EmployeeLastName>Doe</EmployeeLastName>
		<EmployeeFirstName>Jane</EmployeeFirstName>
	</Employee>
</Employees>
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,055 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,853 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,516 Reputation points Microsoft Vendor
    2023-09-27T03:34:04.2533333+00:00

    Hi @John , Welcome to Microsoft Q&A,

    In fact, if you use this kind of XML, you don't need to use XMLSerializer.

    Because XMLSerializer is typically used to serialize and deserialize objects, rather than populating XML data directly into controls.

    You can use XmlDocument to parse the XML and populate the data into the control.

    Such as:

    private void Form1_Load(object sender, EventArgs e)
    {
        //Create an XmlDocument object and load the XML file
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"demo.xml"); // Replace with your XML file path
    
        // Get the values of the EmployeeLastName and EmployeeFirstName elements under the Employee node
        XmlNode lastNameNode = xmlDoc.SelectSingleNode("/Employees/Employee/EmployeeLastName");
        XmlNode firstNameNode = xmlDoc.SelectSingleNode("/Employees/Employee/EmployeeFirstName");
    
        if (lastNameNode != null && firstNameNode != null)
        {
            // Assign data to labels and text boxes
            label1.Text = "Last Name:" + lastNameNode.InnerText;
            textBox1.Text = lastNameNode.InnerText;
            textBox2.Text = firstNameNode.InnerText;
        }
        else
        {
            MessageBox.Show("The XML data is incomplete or incorrectly formatted.");
        }
    }
    

    enter image description here

    Using XmlDocument is more flexible and can also control xml well (directly manipulate XML documents, such as adding, deleting or modifying nodes). And it doesn't require defining a model. But it does not support automatic serialization and deserialization and requires manual parsing.

    If you are dealing with XML data that closely matches the C# model and only require simple serialization and deserialization operations, then XmlSerializer may be more suitable.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.