2016-07-25 32 views
1

我的web服務是給格式化使用對象到XML序列化XML JSON字符串。我的效應初探像:無法解析JSON響應字符串以XML

public static string ObjectToXmlString(this Object obj) 
    { 
     string xmlStr = string.Empty; 
     XmlSerializer xs = new XmlSerializer(obj.GetType()); 
     XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
     ns.Add("", ""); 
     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      XmlWriterSettings settings = new XmlWriterSettings() 
      { 
       Encoding = Encoding.UTF8, 
       Indent = false, 
       OmitXmlDeclaration = true, 
       NewLineChars = string.Empty, 
       NewLineHandling = NewLineHandling.None 
      }; 
      using (XmlWriter writer = XmlWriter.Create(memoryStream, settings)) 
      { 
       xs.Serialize(writer, obj,ns); 
      } 
      return Encoding.UTF8.GetString(memoryStream.ToArray()).Replace("\"", string.Empty); 
     } 
    } 

服務響應:

"<ArrayOfOwnerShipDetails><OwnerShipDetails><OwnerShipId>80932</OwnerShipId><FileNumber>rp1144</FileNumber><Salutation>Mr</Salutation><OwnerFirstName>Jai Kumar Datwni ji</OwnerFirstName><OwnerMiddleName /><OwnerLastName /><ResidentialAddress>1159 Sec 18 C,c Hd</ResidentialAddress><EmailID /><MobileNumber /><VoterID /><AadharCardNo /><RelationCode>S</RelationCode><RelationDescription>Son of</RelationDescription><FatherOrHusbandName>Lachman Dass S</FatherOrHusbandName><PropertyShareInPercentage>50.00</PropertyShareInPercentage><AdditionalRemarks /></OwnerShipDetails></ArrayOfOwnerShipDetails>" 

但是,當我嘗試解析此JSON響應其他應用程序,其中我打電話我的服務。

using (var client = new HttpClient()) 
       { 
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); 
        var response = client.GetAsync("http://localhost:50429/api/Haris/GetOwnersByFileNo?fileNo=RP1144").Result; 

        if (response.IsSuccessStatusCode) 
        { 
         // by calling .Result you are performing a synchronous call 
         var responseContent = response.Content; 

         // by calling .Result you are synchronously reading the result 
         string responseString = responseContent.ReadAsStringAsync().Result.Trim(); 


         // Create the XmlDocument. 
         XmlDocument doc = new XmlDocument(); 
         **doc.LoadXml(responseString); ---> Error Comes here** 


        } 
       } 

它給錯誤 數據在根級別是無效的。 1號線,位置1

但是,如果我在運行時相同的效應初探複製到記事本,然後粘貼到變量它工作正常。 json字符串中出現雙引號有什麼問題嗎?

請幫我在這裏。

回答

0

使用Load()方法來代替,它會解決這個問題。

string sb = responseString; 
         sb = responseString.Replace("\"", string.Empty).Trim().ToString().Trim(); 
         var a = GenerateStreamFromString(sb); 
         StreamReader reader = new StreamReader(a); 
         string text = reader.ReadToEnd(); 
         XmlDocument doc = new XmlDocument(); 
         doc.LoadXml(responseString); 

在此之後,我發現這個解決方案,非常合身:See more

否則

doc.LoadXml(responsestring.Substring(responsestring.IndexOf(Environment.NewLine))); 

Live Demo

+0

我都試過,但都給出了錯誤:.Load()給出--->路徑中具有非法字符。和responsestring.Substring(responsestring.IndexOf(Environment.NewLine)給出----> {「StartIndex不能小於零。\ r \ nParameter name:startIndex」} – Gerry

+0

在加載前使用@之前的xml。 – Developer

+0

之前的xml字符串我正在傳遞給.Load()? – Gerry