Getting Null Values in objects [Resolved]

Posted by Self-Innovator under XML on 4/19/2016 | Points: 10 | Views : 252 | Status : [Member] | Replies : 3
Hi All,

I'm trying to read an XML data in to my c# object i am recieving a null values please find the code below and my XML output

 private LogisticsReport GetLogisticsReport()
{
int StoreId = 00316;
try
{
XmlDocument xmlDoc = new XmlDocument();

string LogisticsReportResponseXml = _reportService.GetLogisticsReportInfo(StoreId).Result;
xmlDoc.LoadXml(LogisticsReportResponseXml);

var logisticsReport = new LogisticsReport();
//check for a an "error" node within the results
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://wsdev.truserv.com/oraclewebservice/oraclemdb.asmx");
XmlNode resultNode = xmlDoc.SelectSingleNode("results", nsmgr);

XmlNode userNode = resultNode.SelectSingleNode("logistics", nsmgr);
if (userNode != null)
{
logisticsReport.FreightRate = userNode.Attributes["freightrate"] != null ? userNode.Attributes["freightrate"].Value : string.Empty;
logisticsReport.OffShoreDelivery = userNode.Attributes["offshoredelivery"] != null ? userNode.Attributes["offshoredelivery"].Value : string.Empty;
logisticsReport.StartCode = userNode.Attributes["stratcode"] != null ? userNode.Attributes["stratcode"].Value : string.Empty;
logisticsReport.MultipleDelivery = userNode.Attributes["multipledelivery"] != null ? userNode.Attributes["multipledelivery"].Value : string.Empty;
logisticsReport.Palletized = userNode.Attributes["palletized"] != null ? userNode.Attributes["palletized"].Value : string.Empty;
logisticsReport.TotesShipped = userNode.Attributes["totesshipped"] != null ? userNode.Attributes["totesshipped"].Value : string.Empty;
logisticsReport.TotesReturned = userNode.Attributes["totesreturned"] != null ? userNode.Attributes["totesreturned"].Value : string.Empty;
logisticsReport.TotesLastDate = userNode.Attributes["toteslastdate"] != null ? userNode.Attributes["toteslastdate"].Value : string.Empty;
logisticsReport.ContainerShipped = userNode.Attributes["containersshipped"] != null ? userNode.Attributes["containersshipped"].Value : string.Empty;
logisticsReport.ContainersReturned = userNode.Attributes["containersreturned"] != null ? userNode.Attributes["containersreturned"].Value : string.Empty;
logisticsReport.ContainersLastDate = userNode.Attributes["containerslastdate"] != null ? userNode.Attributes["containerslastdate"].Value : string.Empty;
}

return logisticsReport;
}
catch (Exception ex)
{
throw ex;
}
}


XML OutPut

<results>
<response mode="all" value=""/>
<logistics>
<freight
rdc1="01 - HARVARD"
rdc2="01 - HARVARD"
freightrate="0 %"
offshoredelivery ="Y"
stratcode="Y"
multipledelivery="N"
palletized="N"
totesshipped="0"
totesreturned="0"
toteslastdate="5/30/2001 12:00:00 AM"
containersshipped="0"
containersreturned="0"
containerslastdate="5/30/2001 12:00:00 AM"/>
<routes/>
</logistics>
</results>

Appreciate your response.

Thanks in Advance

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Rajnilari2015 on: 4/20/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this
using System;
using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"input.xml");

foreach (var element in doc.Descendants("freight"))
{
Console.WriteLine("{0} = {1}", "rdc1", element.Attribute("rdc1").Value);
Console.WriteLine("{0} = {1}", "rdc2", element.Attribute("rdc2").Value);
Console.WriteLine("{0} = {1}", "freightrate", element.Attribute("freightrate").Value);
Console.WriteLine("{0} = {1}", "offshoredelivery", element.Attribute("offshoredelivery").Value);
Console.WriteLine("{0} = {1}", "stratcode", element.Attribute("stratcode").Value);
Console.WriteLine("{0} = {1}", "multipledelivery", element.Attribute("multipledelivery").Value);
Console.WriteLine("{0} = {1}", "palletized", element.Attribute("palletized").Value);
Console.WriteLine("{0} = {1}", "totesshipped", element.Attribute("totesshipped").Value);
Console.WriteLine("{0} = {1}", "totesreturned", element.Attribute("totesreturned").Value);
Console.WriteLine("{0} = {1}", "toteslastdate", element.Attribute("toteslastdate").Value);
Console.WriteLine("{0} = {1}", "containersshipped", element.Attribute("containersshipped").Value);
Console.WriteLine("{0} = {1}", "containersreturned", element.Attribute("containersreturned").Value);
Console.WriteLine("{0} = {1}", "containerslastdate", element.Attribute("containerslastdate").Value);
}

Console.ReadKey();
}
}
}


--
Thanks & Regards,
RNA Team

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/20/2016 [Member] Bronze | Points: 25

Up
0
Down
Hi Rajani,

Thanks i have also got one more idea to resolve this as below

public ActionResult GetLogisticsReport(int StoreId)
{
StoreId = 00316;
try
{
XmlDocument xmlDoc = new XmlDocument();

string LogisticsReportResponseXml = _reportService.GetLogisticsReportInfo(StoreId).Result;
xmlDoc.LoadXml(LogisticsReportResponseXml);

var logisticsInfoViewModel = new LogisticsInfoViewModel();

//check for a an "error" node within the results
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ns", "http://wsdev.truserv.com/oraclewebservice/oraclemdb.asmx");
XmlNode resultNode = xmlDoc.SelectSingleNode("results", nsmgr);

XmlNode userNode = resultNode.SelectSingleNode("logistics/freight", nsmgr);
if (userNode != null)
{
logisticsInfoViewModel.FreightRate = userNode.Attributes["freightrate"] != null ? userNode.Attributes["freightrate"].Value : string.Empty;
logisticsInfoViewModel.OffShoreDelivery = userNode.Attributes["offshoredelivery"] != null ? userNode.Attributes["offshoredelivery"].Value : string.Empty;
logisticsInfoViewModel.StartCode = userNode.Attributes["stratcode"] != null ? userNode.Attributes["stratcode"].Value : string.Empty;
logisticsInfoViewModel.MultiDelivery = userNode.Attributes["multipledelivery"] != null ? userNode.Attributes["multipledelivery"].Value : string.Empty;
logisticsInfoViewModel.Palletized = userNode.Attributes["palletized"] != null ? userNode.Attributes["palletized"].Value : string.Empty;
logisticsInfoViewModel.TotesShipped = userNode.Attributes["totesshipped"] != null ? userNode.Attributes["totesshipped"].Value : string.Empty;
logisticsInfoViewModel.TotesReturned = userNode.Attributes["totesreturned"] != null ? userNode.Attributes["totesreturned"].Value : string.Empty;
logisticsInfoViewModel.TotesLastDate = userNode.Attributes["toteslastdate"] != null ? userNode.Attributes["toteslastdate"].Value : string.Empty;
logisticsInfoViewModel.ContainersShipped = userNode.Attributes["containersshipped"] != null ? userNode.Attributes["containersshipped"].Value : string.Empty;
logisticsInfoViewModel.ContainersReturned = userNode.Attributes["containersreturned"] != null ? userNode.Attributes["containersreturned"].Value : string.Empty;
logisticsInfoViewModel.ContainersLastDate = userNode.Attributes["containerslastdate"] != null ? userNode.Attributes["containerslastdate"].Value : string.Empty;
XmlNode routeNode = resultNode.SelectSingleNode("logistics/routes/route", nsmgr);
if(routeNode!=null)
{
logisticsInfoViewModel.DayOfTheWeek = routeNode.Attributes["dayofweek"] != null ? routeNode.Attributes["dayofweek"].Value : string.Empty;
logisticsInfoViewModel.GroupNo = routeNode.Attributes["groupnbr"] != null ? routeNode.Attributes["groupnbr"].Value : string.Empty;
logisticsInfoViewModel.TruckRouteId = routeNode.Attributes["truckrouteid"] != null ? routeNode.Attributes["truckrouteid"].Value : string.Empty;
logisticsInfoViewModel.OrderPickRequest = routeNode.Attributes["orderpickseq"] != null ? routeNode.Attributes["orderpickseq"].Value : string.Empty;
logisticsInfoViewModel.TruckLoadSeq = routeNode.Attributes["truckloadseq"] != null ? routeNode.Attributes["truckloadseq"].Value : string.Empty;
logisticsInfoViewModel.TruckLoadSeq2 = routeNode.Attributes["truckloadseq2"] != null ? routeNode.Attributes["truckloadseq2"].Value : string.Empty;
logisticsInfoViewModel.Shift = routeNode.Attributes["shift"] != null ? routeNode.Attributes["shift"].Value : string.Empty;
}

}

return PartialView("_logisticsReport", logisticsInfoViewModel);
}








My XML


<results>
<response mode="all" value=""/>
<logistics>
<freight rdc1="01 - HARVARD" rdc2="01 - HARVARD" freightrate="5.31 %" offshoredelivery="N" stratcode="J" multipledelivery="N" palletized="Y" totesshipped="17875" totesreturned="17891" toteslastdate="11/28/2015 12:00:00 AM" containersshipped="0" containersreturned="0" containerslastdate="11/28/2015 12:00:00 AM" />
<routes>
<route dayofweek="Sunday" groupnbr="8" truckrouteid="AF" truckloadseq="23" orderpickseq="C " truckloadseq2="#" shift="1" />
</routes>
</logistics>
</results>


<div class="col-md-12">
<h3>Select Report</h3>
@Html.DropDownList("bindReports", new List<SelectListItem>()
{
new SelectListItem() { Text= "Consolidated Purchases", Value = "cp" },
new SelectListItem() { Text= "General Information", Value = "gi" },
new SelectListItem() { Text= "Logistics", Value = "lg" },
new SelectListItem() { Text= "Programs", Value = "pr" }
}, "Select Report")
<a href="#" class="red-btn">Print Report</a>
</div>
<div id="_genericReport">
</div>
<div class="general-info">
</div>


$("#bindReports").change(function () {
var webUrl;
var dropval = $("#bindReports").val();
if (dropval == "Select Report") {
alert("Pls select")
}
else {
if (dropval == "lg") {
webUrl = "GetLogisticsReport"
}
if (dropval == "pr") {
webUrl = "GetPrograms

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-Innovator on: 4/20/2016 [Member] Bronze | Points: 25

Up
0
Down
One more solution i found

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response