我正在尝试从Web服务反序列化响应。
性反应:
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<currency>
<id><![CDATA[1]]></id>
<name><![CDATA[Złoty]]></name>
<iso_code><![CDATA[PLN]]></iso_code>
<iso_code_num><![CDATA[985]]></iso_code_num>
<blank><![CDATA[1]]></blank>
<sign><![CDATA[zł]]></sign>
<format><![CDATA[2]]></format>
<decimals><![CDATA[1]]></decimals>
<conversion_rate><![CDATA[1.000000]]></conversion_rate>
<deleted><![CDATA[0]]></deleted>
<active><![CDATA[1]]></active>
</currency>
</prestashop>
我的班级代码:
public class prestashop
{
Currency currency;
}
public class Currency
{
public int id;
public string name;
public string iso_code;
public int iso_code_num;
public int blank;
public string sign;
public int format;
public int decimals;
public float conversion_rate;
public bool deleted;
public bool active;
}
最后我尝试反序列化的部分:
Prestashop_WebClient client = new Prestashop_WebClient();
prestashop objpresta = new prestashop();
StreamReader strumien = client.ReadRecord("currencies", "1");
XmlSerializer ser = new XmlSerializer(typeof(prestashop));
objpresta = (prestashop)ser.Deserialize(strumien);
strumien.Close();
当我运行代码时,反序列化后对象objpresta为null但没有错误。
我会非常感谢任何帮助。
答案 0 :(得分:0)
试试这个......
Usings .....
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
类.....
[XmlRoot(ElementName = "currency")]
public class Currency
{
[XmlElement(ElementName = "id")]
public string Id { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "iso_code")]
public string Iso_code { get; set; }
[XmlElement(ElementName = "iso_code_num")]
public string Iso_code_num { get; set; }
[XmlElement(ElementName = "blank")]
public string Blank { get; set; }
[XmlElement(ElementName = "sign")]
public string Sign { get; set; }
[XmlElement(ElementName = "format")]
public string Format { get; set; }
[XmlElement(ElementName = "decimals")]
public string Decimals { get; set; }
[XmlElement(ElementName = "conversion_rate")]
public string Conversion_rate { get; set; }
[XmlElement(ElementName = "deleted")]
public string Deleted { get; set; }
[XmlElement(ElementName = "active")]
public string Active { get; set; }
}
[XmlRoot(ElementName = "prestashop")]
public class Prestashop
{
[XmlElement(ElementName = "currency")]
public Currency Currency { get; set; }
[XmlAttribute(AttributeName = "xlink", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xlink { get; set; }
}
代码.....
string strXML = File.ReadAllText("xml.xml");
byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
MemoryStream ms1 = new MemoryStream(bufXML);
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(Prestashop));
try
{
using (XmlReader reader = new XmlTextReader(ms1))
{
Prestashop deserializedXML = (Prestashop)serializer.Deserialize(reader);
}// put a break point here and mouse-over Label1Text and Label2Text ….
}
catch (Exception ex)
{
throw;
}
您的XML
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<currency>
<id><![CDATA[1]]></id>
<name><![CDATA[Złoty]]></name>
<iso_code><![CDATA[PLN]]></iso_code>
<iso_code_num><![CDATA[985]]></iso_code_num>
<blank><![CDATA[1]]></blank>
<sign><![CDATA[zł]]></sign>
<format><![CDATA[2]]></format>
<decimals><![CDATA[1]]></decimals>
<conversion_rate><![CDATA[1.000000]]></conversion_rate>
<deleted><![CDATA[0]]></deleted>
<active><![CDATA[1]]></active>
</currency>
</prestashop>
我正在从名为xml.xml的应用程序构建文件夹中的文件中读取XML到一个字符串...您需要从其他地方获取XML字符串或创建xml.xml文件并保存XML上面的代码工作
答案 1 :(得分:0)
您正在拼写所有属性,就像XML元素一样,因此不需要为每个属性添加[XmlElement]
。但我认为XmlRootAttribute
缺失了:
[XmlRoot("currency")]
public class Currency
...