Solved

From table to XML in c#

Posted on 2014-11-23
8
195 Views
Last Modified: 2016-02-18
Starting with a table like:

Table1
==================================
FieldNames      UserName         include          index
------------           ---------------         ---------------      ---------
First_Name     default                 true             1
Last_Name      default                 true             2
City                   default                 true             3

etc.

Question: How to read it in c# and store it in XML like:

<?xml version="1.0" encoding="utf-8" ?>
<Users>
     <User Name="Default">
         <Settings>
              <Setting SettingName = "Default">
                     <Field_Name="First_Name" Text ="All" Include="Yes" Index="1"></Field_Name>
                     <Field_Name="Last_Name" Text ="All" Include="Yes" Index="2"></Field_Name>
                    <Field_Name="City" Text ="All" Include="Yes" Index="3"></Field_Name>
             </Setting>
        </Settings>
    </User>

</Users>
0
Comment
Question by:Mike Eghtebas
  • 4
  • 3
8 Comments
 
LVL 5

Assisted Solution

by:ashokpumca
ashokpumca earned 100 total points
ID: 40461412
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40462208
Hi eghtebas;

Can you give a little more detail.

Is the Table a SQL table or is it a DataTable object?

Will the table have more then one User identifying all its settings? If so will the UserName column be the same for  each user and her/his settings?

Where are you picking up the attribute SettingName for each user?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40462462
Is the Table a SQL table or is it a DataTable object?
- The data comes from a Proc spGetFieldNames

Will the table have more then one User identifying all its settings?
-No, it will have only one user. (FYI: User named default is copied to any new user like User1, User2, etc. which will be handled later.)

In the above example, <Setting SettingName = "Default"> really should be reading <Setting SettingName = "DefaultSetting"> this allows, for User1 for example, to have (but for this question we concern with this. I have it here just for information):

  <User Name="User1">
         <Settings>
              <Setting SettingName = "User1Setting1">
                     .
                     .
             </Setting>
        </Settings>
         <Settings>
              <Setting SettingName = "User1Setting2">
                     .
                     .
             </Setting>
        </Settings>
    </User>

Open in new window


User1 has the option of just using the DefaultSetting copied under her/his name to this xml file (in which case is renamed "User1Setting1" with options like:
-Change the setting name to something more meaningful.
- Add new setting configured differently.

If so will the UserName column be the same for  each user and her/his settings?
Yes, When we talk about UserName column, we are referring to the data provided via the proc (spGetFieldNames) where the user name always will be "Default" or DefaultUser.

After this file is copied to the xml file, via some future methods in asp.net c#, this default settings will be copied under the users one or more times each named User1Setting1, User1Setting2,...  User2Setting1, User2Setting2,... etc.

Where are you picking up the attribute SettingName for each user?
As mentioned above, I had my SettingName="Default" which I changed it to SettingName=DefaultSetting". The table in the Proc will contain only SettingName=DefaultSetting" and UserName="DefaultUser".

New user later is created along with one or many setting options for each user. But, this is out of scope in this question.

Mike
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40462485
Ashok,

Thank you for the link. I visited and find it very useful. I will add a comment at your site to show my appreciation.

Mike
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40462578
I just noticed that these three node have an invalid format

<Field_Name="First_Name" Text ="All" Include="Yes" Index="1"></Field_Name>
<Field_Name="Last_Name" Text ="All" Include="Yes" Index="2"></Field_Name>
<Field_Name="City" Text ="All" Include="Yes" Index="3"></Field_Name>

Open in new window

<Field_Name=???? is not a node name and it looks like you want it to be an attribute?
Should be something like
<Field_Name ... Attributes ...> </Field_Name>

What should the format be like?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40462600
Kind of new to xml. The only feedback I have is when a user is known, we will cycle through field names and get its settings. I am open to any kind of change to make this work.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40462721
Fernando,

BTW, I will be adding this proc to a LINQ-TO-SQL data source. In this regard you may consider it will be generating some objects for use.

Mike
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 400 total points
ID: 40462830
Hi Mike;

Here is some sample code to accomplish what you are looking for. This code assumes that the data is in a DataTable object that you fill from a stored procedure as outlined in the question.

// In the below code dt is the DataTable object. Also in the Select part of the statement
// the last string in each line is the column name in the table and must be in the correct
// case.
var results = from u in dt.AsEnumerable()
              select new {
                  FieldName = u.Field<string>("FieldName"),
                  UserName = u.Field<string>("UserName"),
                  Include = u.Field<string>("Include"),
                  Index = u.Field<string>("Index")
              };

// This part of the code builds the static part of the XML document              
XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", null),
    new XElement("Users",
        new XElement("User", new XAttribute("Name", "Default"),
            new XElement("Settings",
                new XElement("Setting", new XAttribute("SettingName", "User1Setting1")))
    )));

// This statement gets a reference to the "Setting" node in the XML document
// so that the following nodes gets inserted into the correct place.
XElement setting = xdoc.Root.Element("User").Element("Settings").Element("Setting");
    
// This part of the code builds the nodes from the data of the DataTable
// and inserts it into the XML document.
foreach (var element in results)
{
    XElement newSetting = new XElement("Field", new XAttribute("Name", element.FieldName),
        new XAttribute("Text", "All"), new XAttribute("Include", (element.Include == "true") ? "Yes" : "No"),
        new XAttribute("Index", element.Index));
    setting.Add(newSetting);
}              

// Save the XML document to the file system. Make sure to change the file path
// and name as you need it.
xdoc.Save("C:\\Working Directory\\UserSettings.xml");

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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