如何读取XML数据并绑定到MVC模型或返回jsondata并绑定特定列

时间:2016-02-19 11:02:38

标签: asp.net-mvc jqgrid xml-parsing

我有一个XML,我需要使用MVC在jqGrid中显示,下面是我的ErrorLogcollection

我想阅读XML元素所在的每一个值,并在cshtml页面的jqGrid中显示数据。

我想要几个列绑定到JqGird,如ErrorLog(CorrelationId,ErrorType),消息(错误),设备(DeviceId,类型)位置(纬度,经度)    那么需要在MVC Controller中完成什么来获取此列并绑定到jqGrid(cshtml)视图页面

<?xml version="1.0" encoding="utf-8"?>
<ErrorLogCollection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xxxx/xxxxx/Inbound/Error">
  <HasNextPage>false</HasNextPage>
  <Nav>
    <Link rel="self" methods="GET" href="https://t21-data.tss/xxxx/Error/Device/99F10001?fromDate=2016-02-09T04:45:00.000Z&toDate=2016-02-09T05:22:34.459Z"/>
  </Nav>
  <ErrorLog>
    <CorrelationId>724397918</CorrelationId>
    <ErrorType>Origin Validation</ErrorType>
    <ErrorMessages>
      <Message>
        <Error>
          Device of Type: PLE631 and DeviceId 99F10001 is not identifiable..
        </Error>
        <Header>
          <Origin>
            <Device>
              <DeviceId>99F10001</DeviceId>
              <Type>PLE631</Type>
            </Device>
          </Origin>
          <TimestampUtc>2016-01-21T05:22:34.459Z</TimestampUtc>
        </Header>
        <Blocks>
          <Location>
            <Latitude>-20.593684</Latitude>
            <Longitude>78.96288</Longitude>
            <IsValid>true</IsValid>
            <PrecisionKilometers>100</PrecisionKilometers>
          </Location>
          <MachineSecuritySystemOperatorID>
            <ID>6578</ID>
            <FirstName>MSSFirstName</FirstName>
            <LastName>MSSLastName</LastName>
            <Description>Chennai</Description>
            <KeyValue/>
          </MachineSecuritySystemOperatorID>
        </Blocks>
        <PayloadCorrelationId>724397918</PayloadCorrelationId>
      </Message>
    </ErrorMessages>
    <InsertUtc>2016-02-09T05:22:21.633Z</InsertUtc>
    <PayloadUrl>
      https://xxxxx/xxx/Payload/724397918/
    </PayloadUrl>
  </ErrorLog>
  <ErrorLog>...</ErrorLog>
  <ErrorLog>...</ErrorLog>
  <ErrorLog>...</ErrorLog>
  <ErrorLog>...</ErrorLog>
  <ErrorLog>...</ErrorLog>
</ErrorLogCollection> 

cshtml代码&amp; Javascript代码

function getDataInSearchResult(){

$('#jQGrid').jqGrid({


    search: true,       
    multiboxonly: true,
    colNames: ["PayloadCorrelationId", "Export", "ErrorType"],

    colModel: [
        { name: 'ErrorLogCollection.[ErrorLog][CorrelationId]', Index: 'ErrorLogCollection.[ErrorLog][CorrelationId]', width: 120 },
         { name: "Export", editable: true, edittype: "checkbox", width: 45, sortable: false, align: "center", formatter: "checkbox", editoptions: { value: "1:0" }, formatoptions: { disabled: false } },
          { name: 'ErrorLogCollection.[ErrorLog][ErrorType]', index: 'ErrorLogCollection.[ErrorLog][ErrorType]', width: 100 }],

    cellEdit: true,
    pager: jQuery('#divpager'),
    rowList: [10, 20, 50, 100],
    sortorder: "desc",
    datatype: 'json',
    viewrecords: true,
    mtype: 'GET',
    gridview: true,
    url: '/DataIn/DataInSearchResult/',


});
$("#jQGrid").jqGrid('navGrid','#divpager',
              {add:false,edit:false,del:false,search:false,refresh:false},
              {},
              {},
              {},
               { multipleSearch: true, multipleGroup: true, showQuery: true }
             );



 }

MVC代码

public JsonResult DataInSearchResult(string ddlDSVIN,string search,string OEMType,DateTime?frmDate = null,DateTime?toDate = null)         {             XDocument xmlResponse = null;             string strJsonTest = null;             string resourceURI = string.Empty;             xmlResponse = GetMongoErrorForAssetOrDevice(“1”,“99F10001”,“CAT”,frmDate,toDate);             ErrorLogCollection errorLogCollec = new ErrorLogCollection();

      XmlSerializer serializer = new XmlSerializer(typeof(ErrorLogCollection));
      Stream ms = new MemoryStream();
      using (XmlWriter xw = XmlWriter.Create(ms))
      {
          xmlResponse.WriteTo(xw);
      }
      ms.Position = 0;
      errorLogCollec = (ErrorLogCollection)serializer.Deserialize(ms);



        //if (xmlResponse != null)
        //{
        //    DataTable dt = null;
        //    var jsonData = Newtonsoft.Json.JsonConvert.SerializeObject<ErrorLogCollection>(xmlResponse);
        //    return Json(jsonData, JsonRequestBehavior.AllowGet);

        //}
        //else
            return Json("No Error's Found", JsonRequestBehavior.AllowGet);


     }

现在请检查一下并回复我的询问....

我想阅读并将其绑定到jqGrid,请你告诉我如何处理这个过程。

0 个答案:

没有答案