Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Friends,

I have to show the XML data in a panel on my c# winform application.
Something like this:
https://i-msdn.sec.s-msft.com/dynimg/IC463730.jpg[^]

Not sure any concept like 'Document panel' is available in C# or not.

Please provide some ideas, if this is achievable.

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 28-May-15 11:05am    
Why Word? Do you already have a way to show a Word document? I doubt it.
—SA

The question makes no sense at all:

  • You hardly can show a Word document in your application. Why asking about using something you cannot use?
  • Microsoft Word cannot show XML document reasonably well. Why trying to use something which does not solve the problem a all, even if you don't use it in your application?


The component which can show XML reasonably well, is, for example System.Windows.Forms.WebBrowser:
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser%28v=vs.110%29.aspx[^].

It is based on IE which if far from being perfect, but this is the easiest way which can be considered as good enough. The other approach could be using System.Windows.Forms.TreeView; you can parse XML and populate it yourself:
https://msdn.microsoft.com/en-us/library/system.windows.forms.treeview%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v2
As per my understanding, you want to display data on form...
So, what you have to do?
1) create new windows form project
2) add 4 labels and 4 texboxes; change their names to undertandable names, such as: txtCompanyName, txtContactName, txtContactTitle, txtPhone
3) create custom class, for example:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;

public class Customer
{
    private string sCompanyName = string.Empty;
    private string sContactName = string.Empty;
    private string sContactTitle = string.Empty;
    private string sPhone = string.Empty;

    public Customer()
    {
     //do nothing
    }

    public Customer(string _CompanyName, string _ContactName, string _ContactTitle, string _Phone)
    {
        sCompanyName = _CompanyName;
        sContactName = _ContactName;
        sContactTitle = _ContactTitle;
        sPhone = _Phone;
    }

    public void LoadFromXml(string sFileName)
    {
        XDocument xDoc = XDocument.Load(sFileName);
        sCompanyName = xDoc.Root.Descendants().Where(n => n.Name == "CompanyName").Select(n => n.Value).FirstOrDefault();
        sContactName = xDoc.Root.Descendants().Where(n => n.Name == "ContactName").Select(n => n.Value).FirstOrDefault();
        sContactTitle = xDoc.Root.Descendants().Where(n => n.Name == "ContactTitle").Select(n => n.Value).FirstOrDefault();
        sPhone = xDoc.Root.Descendants().Where(n => n.Name == "Phone").Select(n => n.Value).FirstOrDefault();
    }

    public string CompanyName
    {
        get { return sCompanyName; }
        set { sCompanyName = value; }
    }

    public string ContactName
    {
        get { return sContactName; }
        set { sContactName = value; }
    }

    public string ContactTitle
    {
        get { return sContactTitle; }
        set { sContactTitle = value; }
    }

    public string Phone
    {
        get { return sPhone; }
        set { sPhone = value; }
    }
}

4) Now, add this code into Form1 class:
C#
public partial class Form1 : Form
{
    Customer oCust = new Customer();
    public Form1()
    {
        InitializeComponent();
        string sFile = @"FullFileName.xml";
        oCust.LoadFromXml(sFile);
        this.txtCompanyName.Text = oCust.CompanyName;
        this.txtContactName.Text = oCust.ContactName;
        this.txtContactTitle.Text = oCust.ContactTitle;
        this.txtPhone.Text = oCust.Phone;
    }


Note: it's just - very basic - example! You can edit data, but nothing will be saved into xml. You need to provide method to change oCust object and save data into xml.

I'd suggest to read about:
1) XmlSerialization and XmlDeserialization[^] to be able to serialize xml to object and back to xml.
2) DataBindings[^]

Try!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900