Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i'm getting the xml values as null kinldy help.

C#
public logistics GetGeneralInfo(int StoreId)
       {
           var storeInfoWebService = new UATStoreReportServiceReference.oraclemdb1SoapClient("oraclemdb1Soap");
           var logisticsInfoFromService = storeInfoWebService.GetLogistics(StoreId.ToString());
           XmlSerializer serializer = new XmlSerializer(typeof(logistics));
           StringReader rdr = new StringReader(logisticsInfoFromService);
           logistics resultingMessage = (logistics)serializer.Deserialize(rdr);
           return resultingMessage;

       }


This is my XML string coming in variable logisticsInfoFromService as

XML
<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 />
</freight></logistics>
</results>


I'm gettting the values for freightrate and OffshreDelivery as NULL

What I have tried:

I have tried by deserializing the data but it was sending the values as NULL
Posted
Updated 20-Apr-16 0:41am
v2
Comments
Sergey Alexandrovich Kryukov 17-Apr-16 23:25pm    
You XML string is already a "C# object". :-)
—SA
sahmed3 18-Apr-16 1:00am    
Hi now i have modified my methods to return a string , i'm getting data in my controller now i need to bind in my view, im new to MVC could you pls help me in that

public ActionResult Manage()
{
return View("Index", GetStoreReports());
}
public LogisticsInfoViewModel GetStoreReports()
{

var generalReportAccess = new LogisticsInfoViewModel();

var storeProfileList = _storeProfileRepository.GetGeneralInfoReport();

XDocument xDoc=new XDocument();

var logisticsModel = from x in xDoc.Descendants("logistics")
select new LogisticsInfoViewModel
{
FreightRate = (string)x.Element("freightrate"),
OffShoreDelivery = (string)x.Element("offshoredelivery")
};

return null;
}

The problem may be that your XML file is not an XML file.

Otherwise, using the debugger will show you exactly what is going on.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Apr-16 23:41pm    
Ah, true, not XML at all... :-) 5ed. :-)
I'm too trusty; if someone tells me "I have an XML file", I tend to trust this person and assume it's XML. But this is totally wrong, nothing should be assumed.

I credited your solution is Solution 2; but whole discussion doesn't worth words. :-)

—SA
Patrice T 17-Apr-16 23:48pm    
Thank you.
The structure of your XML shows that it's schema is really a bad idea. It's pretty much plain home-baked text inside XML.

[EDIT]

Wait a minute… This is not XML file at all. It's not even well-formed; ppolymorphe brought it to my attention in Solution 1. Well, you know… there is nothing to discuss.

[END EDIT]

I would advise stop using obsolete and ugly XmlSerializer and switch to a really good thing, Data Contract:
Using Data Contracts[^].

It's much more robust and easier to use. First of all, you don't have to deal with anything XML-specific by yourself. You just create your data classes and tell the serializer what to serialize and what not; it's a totally non-intrusive and data model agnostic technology, too.

—SA
 
Share this answer
 
v2
Comments
Patrice T 17-Apr-16 23:50pm    
5ed too :-)
sahmed3 18-Apr-16 1:01am    
Hi now i have modified my methods to return a string , i'm getting data in my controller now i need to bind in my view, im new to MVC could you pls help me in that

public ActionResult Manage()
{
return View("Index", GetStoreReports());
}
public LogisticsInfoViewModel GetStoreReports()
{

var generalReportAccess = new LogisticsInfoViewModel();

var storeProfileList = _storeProfileRepository.GetGeneralInfoReport();

XDocument xDoc=new XDocument();

var logisticsModel = from x in xDoc.Descendants("logistics")
select new LogisticsInfoViewModel
{
FreightRate = (string)x.Element("freightrate"),
OffShoreDelivery = (string)x.Element("offshoredelivery")
};

return null;
}

How can i bind my controller's XML data to my view from the above code approach kindly suggest
Patrice T 18-Apr-16 1:07am    
If you want everyone to see this, use Improve question to update your question.
Or open a new question
Sergey Alexandrovich Kryukov 18-Apr-16 2:18am    
Thank you.
—SA
May be the conversion from the xml to the object is not happening properly. Do you have access to the logistics class? Can you check how the Deserializer deserializes your freightrate and OffshreDelivery properties from the xml to object or what ever type they are? by that i mean what is the type of freightrate and OffshreDelivery properties and are they expecting the values that are returned?
 
Share this answer
 
Comments
sahmed3 20-Apr-16 6:41am    
hi saraswati,

Thanks for your reesponse, i have tried it now differently to pass it with in actionresult also i have updated in solution
I have updated the code as below

C#
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 Output

HTML
<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>


Java script function

JavaScript
$("#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 = "GetProgramsReport"
               }
               $.ajax(
                   {
                       type: 'GET',
                       data: { 'StoreId': '00316' },
                       datatype: "json",
                       url: webUrl,
                       success: function (data) {
                           var json = data;
                           console.log(json);
                           $('#_genericReport').html('');
                           $('#_genericReport').html(data);

                       },
                       error: function (result) {
                           alert('Failed to load the result');
                       }
                   })
           }
       });
 
Share this answer
 
v2

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