Solved

C# XML File to Datatable

Posted on 2013-05-20
15
388 Views
Last Modified: 2013-05-20
Hi, I have an XML file like this.

<?xml version="1.0" encoding="utf-8" ?>
<rootelement>
  <titleform>
    <formname>title</formname>
    <controlname>tbxdescription</controlname>
    <visible>true</visible>
    <requiredfiled>true</requiredfiled>
  </titleform>
  <Memberform>
    <formname>member</formname>
    <controlname>tbxmemberid</controlname>
    <visible>false</visible>
    <requiredfiled>false</requiredfiled>
  </Memberform>
</rootelement>


now in C# I need output as follows. I Should be able to pass the form name through code and desired output should result in datatable. Example: If I pass formname as title, member should not appear in datatable. How do I do this ?

FormName      ControlName      RequiredField      Visible
title                     tbxtitle               true                      true
Member             tbxmemberid       false              false
0
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39181368
 using System.Data;
using System.Linq;
using System.Xml.Linq;

 class Program
    {
        static void Main(string[] args)
        {
DataTable dt = new DataTable();
            var root = XElement.Load(@"c:\temp\1.xml");
            dt.Columns.AddRange(root.Elements().FirstOrDefault().Elements().Select(n => new DataColumn { ColumnName = n.Name.LocalName }).ToArray());
            var rows = root.Elements().Select(n => n.Elements().Select(y => y.Value).ToArray());

            foreach (var row in rows)
            {
                dt.Rows.Add(row);
            }
}
}

Open in new window

0
 

Author Comment

by:GRChandrashekar
ID: 39181389
This is not answered

Example: If I pass formname as title, member should not appear in datatable. How do I do this ?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39181395
i don't understand.
what do u mean u pass formname as titleform?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:GRChandrashekar
ID: 39181403
Refer this.

<titleform>
    <formname>title</formname>
    <controlname>tbxdescription</controlname>
    <visible>true</visible>
    <requiredfiled>true</requiredfiled>
  </titleform>

I should be able to pass title as input. Then output should be

FormName      ControlName      RequiredField      Visible
title                     tbxtitle               true                      true
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39181426
so if the value of formname element is "title" then it shouldn't be included in the datatable?
0
 

Author Comment

by:GRChandrashekar
ID: 39181452
If the value for formname element is "title" then "member" should not be included in datatable
If the value for formname element is "member" then "title" should not be included in datatable
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39181519
sorry u gotta be more clear.
there are 2 elements in your xml: titleform and Memberform.
now please describe the use cases.
which element is not suppose to be insert to the datatable? and when?
0
 

Author Comment

by:GRChandrashekar
ID: 39181529
from C# if i pass value as titleform, then output should be

FormName      ControlName      RequiredField      Visible
title                     tbxtitle               true                    true


from C# if i pass value as memberform, then output should be

FormName      ControlName      RequiredField      Visible
Member             tbxmemberid     false                 false
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39181545
>>from C# if i pass value as titleform
what is that mean?
pass where?
0
 

Author Comment

by:GRChandrashekar
ID: 39181600
should be something like

var root = XElement.Load(@"c:\temp\1.xml\title");

I mean pass to XML
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39181666
U cant pass variable to xml like this.
U can have a function which accept either title or member, and the function will populate datatable accordingly.
What i failed to understand is what should the results.
0
 

Author Comment

by:GRChandrashekar
ID: 39181669
Result is here

from C# if i pass value as titleform, then output should be

FormName      ControlName      RequiredField      Visible
title                     tbxtitle               true                    true


from C# if i pass value as memberform, then output should be

FormName      ControlName      RequiredField      Visible
Member             tbxmemberid     false                 false
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39182188
DataTable GetFormData(string formtype){
DataTable dt = new DataTable();
            var root = XElement.Load(@"c:\temp\1.xml");
            dt.Columns.AddRange(root.Elements().FirstOrDefault().Elements().Select(n => new DataColumn { ColumnName = n.Name.LocalName }).ToArray());
            var rows = root.Elements().Select(n => n.Elements().Select(y => y.Value).ToArray()).ToList();

if(formtype == "title"){
                dt.Rows.Add(rows[0]);
}else if(formtype == "member"){
                dt.Rows.Add(rows[1]);
}
return dt;
}

Open in new window

0
 

Author Comment

by:GRChandrashekar
ID: 39182294
These lines (rows[1]); (rows[0]);in resharper says Co-variant conversion from string to object  can cause run time exception on write operation
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39182312
according to the xml u posted u have 2 inner elements, one for titleform and one for memberform.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Code works but breaks when I add one section 4 35
Selenium and Xpath 4 70
Load XML element 3 43
Health check of winows and webservces 5 52
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question