0
我有下面的XML格式: -序列化XML標籤沒有名字
<?xml version="1.0"?>
<Price xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<amount>
<currency>USD</currency>
100
</amount>
<amount>
<currency>EUR</currency>
50
</amount>
</Price>
的XML值包含在XML根量。我可以知道如何序列化100的價值嗎?
[Serializable]
[XmlRoot("amount")]
public sealed class amount
{
[XmlElement("currency")]
public string currency{ get; set; }
}
class Program
{
static void Main(string[] args)
{
var list = new List<amount> {new amount() {Description = "USD"}, new amount() {Description = "EUR"}};
var serializer = new XmlSerializer(typeof(List<amount>), new XmlRootAttribute("Price"));
var ms = new MemoryStream();
serializer.Serialize(ms, list);
ms.Position = 0;
var result = new StreamReader(ms).ReadToEnd();
}
}
您應該使用['XmlConvert.ToDecimal()'](https://msdn.microsoft.com/en-us/library/system.xml .xmlconvert.todecimal(v = vs.110).aspx)和['XmlConvert.Tostring()'](https://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.tostring(v = vs.110).aspx)(或相應的ToInt32()方法)正確地忽略不變格式的文化和解析。 – dbc
@dbc是的,顯然它會更好。 – xanatos