Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

From table to XML in c#

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
Mike Eghtebas
Asked:
Mike Eghtebas
  • 4
  • 3
2 Solutions
 
ashokpumcaCommented:
0
 
Fernando SotoRetiredCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
Independent Software Vendors: 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!

 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now