XML to Dictionary using LINQ

  • Thread starter Thread starter maylortaylor
  • Start date Start date
M

maylortaylor

I'm new to LINQ, so thank you in advance for the help.

I have this function below which takes in a class (consisting of a string(username) and class(USER)). The User Class is setup as follows.

public class User
{
private string _name;
public string UserName { get; set; }
public List<int> ControlNumber { get; set; }
public User()
{
this.ControlNumber = new List<int>();
}
public User (string username) : this()
{
_name = username;
}
}

The XML that is being read into the Class looks like this.

-<UserClassDictionary>
-<User id="asnyder">
-<ControlNumbers>
<ControlNumber>1297267</ControlNumber>
</ControlNumbers>
</User>
-<User id="awatson">
-<ControlNumbers>
<ControlNumber>1296151</ControlNumber>
</ControlNumbers>
</User>
</UserClassDictionary>



The function that does this operation was working before I changed the format of the XML code. So that is why i'm asking for your guys help. I just need to change the LINQ to correspond to the new XML format.
static void XMLToDictionary(Dictionary<string,User> UserClassDict, string xmldoc)
{

if (!File.Exists(xmldoc))
{
XDocument doc = new XDocument(new XElement("UserClassDictionary"));

doc.Save(xmldoc);
}
else
{
//maybe needs to check if XML is empty
XDocument xdoc = XDocument.Load(xmldoc);
Console.WriteLine("Moving XML (" + xmldoc + ") data to User Dictionary...");
var users = from u in xdoc.Root.Elements()
where u.Elements().Any()
select new User
{
UserName = u.Name.LocalName,
ControlNumber = u.Elements().Select(cn => (int)cn).ToList()
};

foreach (var user in users)
UserClassDict.Add(user.UserName, user);
}
}
 
select new User
{
UserName = u.Name.LocalName,
ControlNumber = u.Elements().Select(cn => (int)cn).ToList()
};

Looks like this is what you need (untested).

ControlNumber = u.Element("ControlNumbers").Elements("ControlNumber").Select(cn => (int)cn).ToList()
 
I'm new to LINQ, so thank you in advance for the help.

I have this function below which takes in a class (consisting of a string(username) and class(USER)). The User Class is setup as follows.

public class User
{
private string _name;
public string UserName { get; set; }
public List<int> ControlNumber { get; set; }
public User()
{
this.ControlNumber = new List<int>();
}
public User (string username) : this()
{
_name = username;
}
}

The XML that is being read into the Class looks like this.

-<UserClassDictionary>
-<User id="asnyder">
-<ControlNumbers>
<ControlNumber>1297267</ControlNumber>
</ControlNumbers>
</User>
-<User id="awatson">
-<ControlNumbers>
<ControlNumber>1296151</ControlNumber>
</ControlNumbers>
</User>
</UserClassDictionary>



The function that does this operation was working before I changed the format of the XML code. So that is why i'm asking for your guys help. I just need to change the LINQ to correspond to the new XML format.
static void XMLToDictionary(Dictionary<string,User> UserClassDict, string xmldoc)
{

if (!File.Exists(xmldoc))
{
XDocument doc = new XDocument(new XElement("UserClassDictionary"));

doc.Save(xmldoc);
}
else
{
//maybe needs to check if XML is empty
XDocument xdoc = XDocument.Load(xmldoc);
Console.WriteLine("Moving XML (" + xmldoc + ") data to User Dictionary...");
var users = from u in xdoc.Root.Elements()
where u.Elements().Any()
select new User
{
UserName = u.Name.LocalName,
ControlNumber = u.Elements().Select(cn => (int)cn).ToList()
};

foreach (var user in users)
UserClassDict.Add(user.UserName, user);
}
}

Suggestion:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace E
{
public class User
{
public string UserName { get; set; }
public List<int> ControlNumber { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
string xmlstr = @"<UserClassDictionary>
<User id=""asnyder"">
<ControlNumbers>
<ControlNumber>1297267</ControlNumber>
</ControlNumbers>
</User>
<User id=""awatson"">
<ControlNumbers>
<ControlNumber>1296151</ControlNumber>
</ControlNumbers>
</User>
</UserClassDictionary>";
XDocument doc = XDocument.Parse(xmlstr);
Dictionary<string, User> dic = doc.Root.Elements("User")
.Select(u => new
User { UserName = u.Attribute("id").Value,

ControlNumber =
u.Element("ControlNumbers").Elements("ControlNumber") .Select(cn =>
int.Parse(cn.Value)).ToList() })
.ToDictionary(u =>
u.UserName, u => u);
foreach(KeyValuePair<string, User> u in dic)
{
Console.WriteLine("{0} {1}", u.Key,
u.Value.ControlNumber[0]);
}
Console.ReadKey();
}
}
}

Arne
 
Back
Top