Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
My requirement is to check the node is available or not before applying where and select clause.in my code i am checking value is null or not. But in some XML the element itself missing. Can anyone help to modify the code to meet my requirement.
Thanks in advance

C#
public static List<AccruedClass> validaccrued(string  xmlstring)
{
var filtercondition = new string[] {"COR", "GVT", "MUN"};
XDocument doc = XDocument.Parse(xmlstring);
var ac = doc.Root.Descendants("Positions")
.Where(item =>
{
float? accInt = (float?)item.Element("AccruedInterest");
string mod = (string)item.Element("Mod1");
return accInt != null && accInt.Value > 0.0f && mod != null && filtercondition.Contains(mod);
})
.Select(item => new AccruedClass
{
Office = item.Element("Office").Value != null ? item.Element("Office").Value : "0",
Account = item.Element("Account").Value != null ? item.Element("Account").Value : "0",
KeyAccount = item.Element("KeyAccount").Value != null ? item.Element("KeyAccount").Value : "0",
AccountType = item.Element("AccountType").Value != null ? item.Element("AccountType").Value : "",
MSDWSecurityCode = item.Element("MSDWSecurityCode").Value != null ? item.Element("MSDWSecurityCode").Value : "",
Cusip = item.Element("Cusip").Value != null ? item.Element("Cusip").Value : "",
Mod1 = item.Element("Mod1").Value != null ? item.Element("Mod1").Value : "",
AccruedInterest = item.Element("AccruedInterest").Value != null ? Convert.ToDouble(item.Element("AccruedInterest").Value) : 0,
PriceFactor = item.Element("PriceFactor").Value != null ? Convert.ToDouble(item.Element("PriceFactor").Value) : 0,
Quantity = item.Element("Quantity").Value != null ? Convert.ToDouble(item.Element("Quantity").Value) : 0,

})
.ToList()
.OrderBy(o => o.Cusip);

List<AccruedClass> list_accr = ac.ToList<AccruedClass>();
return list_accr;
}
Posted

1 solution

Use a combination of explicit cast the and null-coalescing operator:
C#
.Select(item => new AccruedClass
{
    Office = (string)item.Element("Office") ?? "0",
    Account = (string)item.Element("Account") ?? "0",
    KeyAccount = (string)item.Element("KeyAccount") ?? "0",
    AccountType = (string)item.Element("AccountType") ?? "",
    MSDWSecurityCode = (string)item.Element("MSDWSecurityCode") ?? "",
    Cusip = (string)item.Element("Cusip") ?? "",
    Mod1 = (string)item.Element("Mod1") ?? "",
    AccruedInterest = (double?)item.Element("AccruedInterest") ?? 0D,
    PriceFactor = (double?)item.Element("PriceFactor") ?? 0D,
    Quantity = (double?)item.Element("Quantity") ?? 0D,
})

If the element / attribute doesn't exist, and the return type is nullable, the explicit cast operator returns null.
 
Share this answer
 
Comments
jinesh sam 24-Jul-15 2:52am    
Thanks its works fine :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900