C# 将响应流转换为XML
我向demo API发送了一篇XML帖子,响应以XML流的形式返回,如下所示:C# 将响应流转换为XML,c#,xml,rest,stream,C#,Xml,Rest,Stream,我向demo API发送了一篇XML帖子,响应以XML流的形式返回,如下所示: API=3CProductData&XML=%3CProductData+Name%3D%22NameTest%22%3E%0D%0A++%3CId%3EXXXXXXXXX%3C%2FId%3E%0D%0A%3C%2FProductData%3E 我猜这就是流的样子,我的目标是获取响应并将其存储在新的ProductData对象中。到目前为止,我已经完成了以下工作: HttpWebResponse r
API=3CProductData&XML=%3CProductData+Name%3D%22NameTest%22%3E%0D%0A++%3CId%3EXXXXXXXXX%3C%2FId%3E%0D%0A%3C%2FProductData%3E
我猜这就是流的样子,我的目标是获取响应并将其存储在新的ProductData对象中。到目前为止,我已经完成了以下工作:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
// as an xml: deserialise into your own object or parse as you wish
StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string receivedResponse = respStream.ReadToEnd();
XmlSerializer x = new XmlSerializer(typeof(ProductData));
ProductData product = (ProductData) x.Deserialize(new StringReader(receivedResponse));
Console.WriteLine("Node1: " + product.Id.ToString());
Console.WriteLine("Node2: " + product.Name);
Console.ReadKey();
}
该错误与System.InvalidOperationException一起返回:“XML文档(0,0)中存在错误。”
XmlException:缺少根元素。必须删除字符串中的部分
API=3CProductData&XML=
,然后解码部分XML
看看这个工作:
string strRegex = @"<ProductData Name=""NameTest"">\r\n <Id>XXXXXXXXX</Id>\r\n</ProductData>";
ProductData result = null;
using (TextReader reader = new StringReader(strRegex))
{
var serializer = new XmlSerializer(typeof(ProductData));
result = (ProductData)serializer.Deserialize(reader);
}
string stregex=@“\r\n xxxxxxxx\r\n”;
ProductData结果=null;
使用(TextReader=new StringReader(stregex))
{
var serializer=新的XmlSerializer(typeof(ProductData));
结果=(ProductData)序列化程序。反序列化(读取器);
}
必须删除字符串中的部分API=3CProductData&XML=
,然后解码部分XML
看看这个工作:
string strRegex = @"<ProductData Name=""NameTest"">\r\n <Id>XXXXXXXXX</Id>\r\n</ProductData>";
ProductData result = null;
using (TextReader reader = new StringReader(strRegex))
{
var serializer = new XmlSerializer(typeof(ProductData));
result = (ProductData)serializer.Deserialize(reader);
}
string stregex=@“\r\n xxxxxxxx\r\n”;
ProductData结果=null;
使用(TextReader=new StringReader(stregex))
{
var serializer=新的XmlSerializer(typeof(ProductData));
结果=(ProductData)序列化程序。反序列化(读取器);
}
这里有两种不同的解决方案
public ProductData TestFunction()
{
ProductData result = new ProductData();
string apiResponse = "API=3CProductData&XML=%3CProductData+Name%3D%22NameTest%22%3E%0D%0A++%3CId%3EXXXXXXXXX%3C%2FId%3E%0D%0A%3C%2FProductData%3E";
string xml = HttpUtility.UrlDecode(apiResponse.Substring(apiResponse.IndexOf("XML=") + 4));
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
XmlNode newNode = document.DocumentElement;
// Name is actually an attribute on the ProductData
result.Name = ((XmlAttribute)newNode.Attributes["Name"]).InnerText;
// Id is an actual node
result.ID = ((XmlNode)newNode.FirstChild).InnerText;
using (TextReader reader = new StringReader(xml))
{
var serializer = new XmlSerializer(typeof(ProductData));
result = (ProductData)serializer.Deserialize(reader);
}
return result;
}
[Serializable]
[XmlRoot("ProductData")]
public class ProductData
{
[XmlElement("Id")]
public string ID { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
}
这段代码有一个非常脆弱的部分,我没有花太多时间去处理它。在我看来,XML的格式不是很好,所以必须在
XML=
后面加上子字符串,这就是我在末尾添加+4的原因。这可能是一种更平滑的方法,但问题实际上是转换XML。由于XML非常简单,您可以通过SelectSingleNode将值作为目标。如果您想使用StreamReader,需要确保您的类/属性设置了属性(即[XmlRoot(“Productdata”)))以下是两种不同的解决方案
public ProductData TestFunction()
{
ProductData result = new ProductData();
string apiResponse = "API=3CProductData&XML=%3CProductData+Name%3D%22NameTest%22%3E%0D%0A++%3CId%3EXXXXXXXXX%3C%2FId%3E%0D%0A%3C%2FProductData%3E";
string xml = HttpUtility.UrlDecode(apiResponse.Substring(apiResponse.IndexOf("XML=") + 4));
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
XmlNode newNode = document.DocumentElement;
// Name is actually an attribute on the ProductData
result.Name = ((XmlAttribute)newNode.Attributes["Name"]).InnerText;
// Id is an actual node
result.ID = ((XmlNode)newNode.FirstChild).InnerText;
using (TextReader reader = new StringReader(xml))
{
var serializer = new XmlSerializer(typeof(ProductData));
result = (ProductData)serializer.Deserialize(reader);
}
return result;
}
[Serializable]
[XmlRoot("ProductData")]
public class ProductData
{
[XmlElement("Id")]
public string ID { get; set; }
[XmlAttribute("Name")]
public string Name { get; set; }
}
这段代码有一个非常脆弱的部分,我没有花太多时间去处理它。在我看来,XML的格式不是很好,所以必须在
XML=
后面加上子字符串,这就是我在末尾添加+4的原因。这可能是一种更平滑的方法,但问题实际上是转换XML。由于XML非常简单,您可以通过SelectSingleNode将值作为目标。如果您想使用StreamReader,您需要确保您的类/属性设置了属性(即[XmlRoot(“Productdata”)])这是您的实际响应主体吗?或者这是来自查询字符串的数据?它是查询字符串,不应该是响应吗well@Jessede Wit还定制了响应,以提供一个XML,但仍然存在相同的问题?这是一些糟糕的XML。名称节点不会自行关闭。恐怕您必须手动处理此响应。查询字符串是(请求的)url的一部分。http响应消息由状态码、头和响应正文组成。在这里发布状态代码和响应正文,我们可能会提供帮助。(将响应体作为字符串读取并在调试器中检查)这是您的实际响应体吗?或者这是来自查询字符串的数据?它是查询字符串,不应该是响应吗well@Jessede Wit还定制了响应,以提供一个XML,但仍然存在相同的问题?这是一些糟糕的XML。名称节点不会自行关闭。恐怕您必须手动处理此响应。查询字符串是(请求的)url的一部分。http响应消息由状态码、头和响应正文组成。在这里发布状态代码和响应正文,我们可能会提供帮助。(以字符串形式读取响应正文,并在调试器中检查)我无法控制流及其外观。我无法控制流及其外观。好的,当我这样做时,它返回“根元素丢失”。有任何建议可以将更新的代码与我的实现一起发布吗?好的,当我这样做时,它返回“缺少根元素。”有什么建议可以在我的实现中发布更新的代码吗?