473,473 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dictionary to XML using LINQ

maylortaylor
72 New Member
So I have a function that takes in a Dictionary and returns a XML document with the information. It uses LINQ which I suck at and would like some help reformat the information.

So far the information comes out like this:

Expand|Select|Wrap|Line Numbers
  1. <UserClassDictionary>
  2.    <adolan>
  3.       <ControlNumber>791301</ControlNumber>
  4.    </adolan>
  5.    <afeazell>
  6.       <ControlNumber>790253</ControlNumber>
  7.    </afeazell>
  8.    <asnyder>
  9.       <ControlNumber>790210</ControlNumber>
  10.       <ControlNumber>790308</ControlNumber>
  11. </UserClassDictionary>
However, I wish to have the format look like this.

Expand|Select|Wrap|Line Numbers
  1. <user name="adolan">
  2. <controlNumbers>
  3. <number>123</number>
  4. <number>546</number>
  5. </controlN​umbers>
  6. </user>

Here is the function thus far.

Expand|Select|Wrap|Line Numbers
  1.  static void DictionaryToXML(Dictionary<string,User> UserClassDict)
  2.         {
  3.             string xmldoc = "UserClassDictionary.xml";
  4.             Console.WriteLine("Moving User Dictionary data to XML (" + xmldoc + ")...");
  5.  
  6.             XElement el = new XElement("UserClassDictionary",
  7.        UserClassDict.Select(kv => new XElement(kv.Key,
  8.                     kv.Value.ControlNumber.Select(num => new XElement("ControlNumber", num)))));
  9.  
  10.             var xml = el.ToString();
  11.             File.WriteAllText(xmldoc, xml);
  12.         }
Jun 7 '13 #1
0 1630

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lacutas | last post by:
Hi all, I am having some problems using LINQ to access Distinct records from a Dataset. I have looked around and believe it should be as simple as added .Distinct() to my LINQ query below, though...
3
by: Ilyas | last post by:
Hi all What is the recommended way for using Linq within a 3 Tier project? Many thanks
10
by: islayer | last post by:
i am trying to write a dictionary using perl, the program would use a primary .pl file, and a text file, designated .dat i have no programing background, and am trying to teach myself perl, and...
2
by: nasayoo | last post by:
I am doing my project using LINQ to SQL technology for the database part. I wrote the coding for inserting a new record into the table. The coding was ok. Its working 100%. But after inserting the...
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
2
by: Big Daddy | last post by:
For example, if I have a DB table called DownloadPoints with a column named DownloadPointNo, then SqlMetal will create a file with a class called DownloadPoints with an accessor named...
0
by: Big Daddy | last post by:
I am trying to do all my DB access through LINQ. For example, I am trying to delete a record from the JobQueue table. There’s a couple ways I could do this: 1. Method 1 – The disadvantage is...
1
by: nadavgg | last post by:
Hi, I have recently developed a C# application using Linq. I am getting from an external database a list of profiles I need to process, some are new and some are already in the database, and need to...
0
by: sagarrokade | last post by:
how can i transfer one object data to another using LINQ or lambda expression?
2
by: urschetan | last post by:
I just want to get an image from sql server database using linq and display that in WPF image box.....I am saving those images to a database directly and i also want to know in which format these...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.