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);
}
}
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);
}
}