Solved

From table to XML in c#

Posted on 2014-11-23
8
182 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSLT Help 12 21
Check my code out(2) 1 33
Round up to 100% in .NET 10 45
Open a word document 23 18
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

21 Experts available now in Live!

Get 1:1 Help Now