从MemoryStream类反序列化XML时,XML文档(27,4)中存在错误

时间:2014-10-20 09:31:49

标签: c# xml memorystream

从API接收XML数据时,我会抛出此异常。

{"字符串''不是有效的AllXsd值。"}

XML文档(27,4)中存在错误

XML数据看起来不错,我之前使用过这段代码来解析类似的XML。所以我不确定导致异常的原因。

返回的XML是

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<agentInventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:N1="demo.org.uk/demo/CustomsStatus" xmlns:N2="demo.org.uk/demo/UnLocation" xmlns:N3="demo.org.uk/demo/AirCarrier" xmlns="demo.org.uk/demo/AgentInventory">
  <shed>TAX</shed>
  <arrivalPort>
    <N2:iataPortCode>LHR</N2:iataPortCode>
  </arrivalPort>
  <masterAirwayBillPrefix>125</masterAirwayBillPrefix>
  <masterAirwayBillNumber>25051155</masterAirwayBillNumber>
  <nominatedAgent>DRB</nominatedAgent>
  <originPort>
    <N2:iataPortCode>BOS</N2:iataPortCode>
  </originPort>
  <destinationPort>
    <N2:iataPortCode>LHR</N2:iataPortCode>
  </destinationPort>
  <airCarrier>
    <N3:carrierCode>BA</N3:carrierCode>
   </airCarrier>
  <flightNumber>123</flightNumber>
  <flightDate>2011-05-25T00:00:00Z</flightDate>
  <npx>10</npx>
  <npr>0</npr>
  <grossWeight>123.0</grossWeight>
  <goodsDescription>BOOKS</goodsDescription>
  <sdc>T</sdc>
  <status1Set></status1Set>
  <status2Set>true</status2Set>
  <ccsCreationTime>2011-05-25T21:17:00Z</ccsCreationTime>
  <customsStatus>
    <N1:code>CC</N1:code>
    <N1:statusText>Customs cleared</N1:statusText>
  </customsStatus>
  <customsSummaryText>TRED CLEARANCE</customsSummaryText>
  <customsSummaryTime>2011-05-25T21:30:00Z</customsSummaryTime>
  <agentReference>RS CFSP </agentReference>
  <isErtsPreArrival>false</isErtsPreArrival>
  <isAgentPreArrival>false</isAgentPreArrival>
  <isDeleted>false</isDeleted>
  <finalised></finalised>
  <createdOn>2011-05-25T21:17:18.167Z</createdOn>
  <modifiedOn>2011-05-25T21:30:20.37Z</modifiedOn>
    </agentInventory>

用于反序列化此XML的代码如下所示:

 var storeXmlInventoryReturnData = returnInventoryData.ReturnValue;

        if (storeXmlInventoryReturnData != null)
        {

            //DeSerialize XML from variable
            agentInventory myInventoryResponse = null;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(agentInventory));
            MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(storeXmlInventoryReturnData));
            myInventoryResponse = (agentInventory)xmlSerializer.Deserialize(memStream);

            Console.WriteLine(
             @"\n\n\n INVENTORY RETURN DATA FOR AWB: {0}-{1} \n\n 
                          Destination Port: {2} \n 
                          Arrival Port: {3} \n 
                          Carrier: {4} \n 
                          Flight No: {5} \n 
                          Flight Date: {6} \n 
                          Customers Status: {7} \n 
                          NPX: {8} \n
                          NPR {9} \n
                          SDC Code: {10}                              
                          \n\n Hit any key to exit...."
                ,

                myInventoryResponse.masterAirwayBillPrefix,
               myInventoryResponse.masterAirwayBillNumber,
                myInventoryResponse.destinationPort,
                myInventoryResponse.arrivalPort,
                myInventoryResponse.airCarrier,
                myInventoryResponse.flightNumber,
               myInventoryResponse.flightDate,
               myInventoryResponse.customsStatus,
                myInventoryResponse.npx,
                myInventoryResponse.npr,
                myInventoryResponse.sdc,
               myInventoryResponse.grossWeight,
                myInventoryResponse.goodsDescription
                    );
            Console.ReadLine();
        }
        else
        {
            Console.Write("No data returned");
        }

抛出异常的行是

myInventoryResponse = (agentInventory)xmlSerializer.Deserialize(memStream);

0 个答案:

没有答案