Solved

From table to XML in c#

Posted on 2014-11-23
8
185 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

943 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now