我的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字符串中出现双引号有什么问题吗?
请帮我在这里。
我都试过,但都给出了错误:.Load()给出--->路径中具有非法字符。和responsestring.Substring(responsestring.IndexOf(Environment.NewLine)给出----> {“StartIndex不能小于零。\ r \ nParameter name:startIndex”} – Gerry
在加载前使用@之前的xml。 – Developer
之前的xml字符串我正在传递给.Load()? – Gerry