在函数ChangeNodeValues中对子XmlNode进行更改后,一切正常,我可以看到调试时所做的更改。但是,在deserializeUnits数组中稍后检入后,将Price属性设置为零。我做错了什么?
var deserializedUnits = new List<XMLImportClasses.Object>();
foreach (XmlNode node in doc.ChildNodes[0].ChildNodes)
{
ChangeNodeValues(node);
using ( MemoryStream stm = new MemoryStream())
{
using (StreamWriter stw = new StreamWriter(stm))
{
stw.Write(node.OuterXml);
stw.Flush();
stm.Position = 0;
XmlSerializer ser = new XmlSerializer(typeof( XMLImportClasses.Object));
XMLImportClasses.Object result = (ser.Deserialize(stm) as XMLImportClasses.Object);
deserializedUnits.Add(result);
}
}
}
private void ChangeNodeValues(XmlNode _node)
{
foreach (XmlNode groupNode in _node.ChildNodes)
{
if(groupNode.Name == "Prices")
foreach (XmlNode priceNode in groupNode.ChildNodes)
{
var strPrice = priceNode.InnerText;
if (strPrice.Count() > 1)
{
strPrice = (Convert.ToInt32(strPrice) + 5).ToString();
priceNode.InnerText = strPrice;
}
}
}
}
编辑:对象,价格和价格等级:
[Serializable()]
public class Price
{
[XmlAttribute( "Type" )]
public string Type { get; set; }
[XmlAttribute( "DateFrom" )]
public DateTime DateFrom { get; set; }
[XmlAttribute( "DateTo" )]
public DateTime DateTo { get; set; }
[XmlElement( "Price" )]
public decimal PriceValue { get; set; }
[XmlElement( "AddPersonPrice" )]
public decimal? AddPersonPrice { get; set; }
[XmlElement( "PerPerson" )]
public decimal? PerPerson { get; set; }
[XmlElement( "Reduction2ndWeek" )]
public decimal? Reduction2ndWeek { get; set; }
}
[Serializable()]
public class Object
{
[XmlAttribute( "id" )]
public string id { get; set; }
[XmlAttribute( "name" )]
public string name { get; set; }
[XmlElement( "Type" )]
public int Type { get; set; }
[XmlElement( "Persons" )]
public int Persons { get; set; }
[XmlElement( "Children" )]
public int? Children { get; set; }
[XmlElement( "Pets" )]
public int? Pets { get; set; }
[XmlElement( "Rooms" )]
public int Rooms { get; set; }
[XmlElement( "Bedrooms" )]
public int Bedrooms { get; set; }
[XmlElement( "Bathrooms" )]
public int Bathrooms { get; set; }
[XmlElement( "Size" )]
public int? Size { get; set; }
[XmlElement( "Stars" )]
public int? Stars { get; set; }
[XmlElement( "ArrivalDays" )]
public string ArrivalDays { get; set; }
[XmlElement( "MinOccupancy" )]
public int? MinOccupancy { get; set; }
[XmlElement( "MinStay" )]
public int? MinStay { get; set; }
[XmlElement( ElementName = "Position", Type = typeof( Position ) )]
public Position Position { get; set; }
[XmlElement( ElementName = "Vacancy", Type = typeof( Vacancy ) )]
public Vacancy Vacancy { get; set; }
[XmlElement( ElementName = "ReservationDates", Type = typeof( ReservationDates ) )]
public ReservationDates ReservationDates { get; set; }
[XmlElement( ElementName = "Prices", Type = typeof( Prices ) )]
public Prices Prices { get; set; }
.
.
.
}
[Serializable()]
public class Prices
{
[XmlElement( ElementName = "Price", Type = typeof( Price ) )]
public Price[] PricesArray { get; set; }
}