I want to select XML elements as following. Please know Data and Party are classes. Can someone help how to achieve following:
select new Data
{
Party.Name = xElem.Element(“Name”).Value,
Party.PostBox = xElem.Element(“PostBox”).Value,
}
With current code, I cannot access properties of Party.
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"c:\test.xml");
var q = from xElem in doc.Descendants("Party")
where (int)xElem.Attribute("ID") == 1
select new Data
{
};
}
public class Data
{
public Party Party { get; set; }
public Data()
{
this.Party = new Party();
}
}
public class Party
{
string name;
string postbox;
public string Name
{
get { return name; }
set { this.name = value; }
}
public string PostBox
{
get { return postbox; }
set { this.postbox = value; }
}
}
@Jon Skeet: Following is the sample code. I get “Object reference not set to an instance of an object” error at runtime only.
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"c:\test\data.xml");
var props = from xElem in doc.Descendants("Party")
where (int)xElem.Attribute("ID") == 1
select new Data
{
Party =
{
Name = xElem.Element("Name").Value.ToString(),
PostBox = xElem.Element("PostBox").Value.ToString(),
Tax =
{
CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
}
}
}
}
public class Data
{
public Party Party { get; set; }
public Data()
{
this.Party= new Party();
}
}
public class Party
{
string name;
string postbox;
public Tax Tax { get; set; }
public string Name
{
get { return name; }
set { this.name = value; }
}
public string PostBox
{
get { return postbox; }
set { this.postbox = value; }
}
}
public class Tax
{
string companyid;
public string CompanyID
{
get { return companyid; }
set { this.companyid = value; }
}
}
@Jon Skeet: Following is the sample code. I get “Object reference not set to an instance of an object” error at runtime only.
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"c:\test\data.xml");
var props = from xElem in doc.Descendants("Party")
where (int)xElem.Attribute("ID") == 1
select new Data
{
Party =
{
Name = xElem.Element("Name").Value.ToString(),
PostBox = xElem.Element("PostBox").Value.ToString(),
Tax =
{
CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
}
}
}
}
public class Data
{
public Party Party { get; set; }
public Data()
{
this.Party= new Party();
}
}
public class Party
{
string name;
string postbox;
public Tax Tax { get; set; }
public string Name
{
get { return name; }
set { this.name = value; }
}
public string PostBox
{
get { return postbox; }
set { this.postbox = value; }
}
}
public class Tax
{
string companyid;
public string CompanyID
{
get { return companyid; }
set { this.companyid = value; }
}
}
You want:
or:
Note that this has nothing to do with XML and nothing really to do with LINQ – it’s just using object initializer features.
One thing you may want to consider is using the explicit conversion from
XElementtostringinstead of usingValue– that way if an element is missing, you get a null reference instead of an exception. It depends on what behaviour you want, but it’s worth knowing about as an option.