Sergey's answer is very useful, because it provides tons of useful information. I'd recommend to read that answer very carefully.
In a short: the best option for you is to use
Xml Serailization[
^].
Why? Because xml serialization and deserialization provides a way to write objects/data into xml and to read it again into objects/data.
To be able to use XML Serialization/Deserialization, you should define classes - for example:
public class User
{
private int id = 0;
private int pin = 0;
private CheckingACC cacc = new CheckingACC();
public int ID
{
get {return id;}
set {id = value;}
}
public int PIN
{
get {return pin;}
set {pin = value;}
}
public CheckingACC CheckingACC
{
get {return cacc;}
set {cacc = value;}
}
}
public class CheckingACC
{
private int n=0;
private double bal=0.0;
public CheckingACC()
{
}
public CheckingACC(int _n, double _b)
{
n = _n;
bal = _b;
}
public int Num
{
get {return n;}
set {n = value;}
}
public double Balance
{
get {return bal;}
set {bal = value;}
}
}
In the same way you should define other classes/properties/members.
For further details, please read these articles:
Examples of XML Serialization[
^]
https://support.microsoft.com/en-us/kb/815813[
^]
XML Serialization and Deserialization: Part-1[
^]
XML Serialization and Deserialization: Part-2[
^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[
^]
Another way is to use XDocument class together with custom classes - as follow:
int id = 123456;
XDocument xdoc = XDocument.Parse(xcontent);
User u = xdoc.Descendants("User")
.Where(x=>(int)x.Element("ID") == id)
.Select(x=> new User{
ID = Convert.ToInt32(x.Element("ID").Value),
PIN = Convert.ToInt32(x.Element("PIN").Value),
CheckingACC = new CheckingACC{
Num = Convert.ToInt32(x.Element("CheckingACC").Element("Num").Value),
Balance = Convert.ToDouble(x.Element("CheckingACC").Element("Balance").Value)
}
})
.SingleOrDefault();
As you can see, above linq query returns custom class (specific object). The magic is in this peace of code:
x=> new User{...}
[EDIT]
I almost forgot to mention that you can return anonymous type:
var query = xdoc.Descendants("User")
.Where(x=>(int)x.Element("ID") == id)
.Select(x=> new {
ID = x.Element("ID").Value,
PIN = x.Element("PIN").Value,
CheckingACC_Num = x.Element("CheckingACC").Element("Num").Value,
CheckingACC_Balance = x.Element("CheckingACC").Element("Balance").Value
});
Above query returns:
ID PIN CheckingACC_Num CheckingACC_Balance
123456 5100 52645852 2500
Good luck!