带有对象和 XSLT 的 XslCompiledTransform
我已向类添加了一些属性和局部变量,并且希望使用 XSLT 将该对象解析为 XML/HTML 文件。
现在出现了奇怪的事情: 当我有这个时:
private double _invoicePrice = 0;
[XmlAttribute("invoicePrice")]
public double InvoicePrice{
get { return _invoicePrice; }
set { _invoicePrice = value; }
}
[XmlAttribute("vat")]
public double Vat
{
get
{
return 4;
/* // also tried this...
double shopVat = 19;
double vat = (_invoicePrice / (shopVat + 100)) * shopVat;
return Math.Round(vat, 2);
*/
}
}
解析不工作
,当我有这个:
private double _invoicePrice = 0;
private double _vat = 0;
[XmlAttribute("invoicePrice")]
public double InvoicePrice{
get { return _invoicePrice; }
set { _invoicePrice = value; }
}
[XmlAttribute("vat")]
public double Vat
{
get { return _vat; }
set { _vat = value; }
}
转换有效!
属性 Vat
在调试时始终有一个值!与两个选项。但不知何故,XslCompiledTransform
需要私有变量或不需要只读变量或其他什么?
有人可以解释为什么选项 2 有效而第一个无效..?
I have added some properties and local variables to a class and would like to parse the object with XSLT to an XML/HTMLfile.
Now here is the strange thing:
when I have this:
private double _invoicePrice = 0;
[XmlAttribute("invoicePrice")]
public double InvoicePrice{
get { return _invoicePrice; }
set { _invoicePrice = value; }
}
[XmlAttribute("vat")]
public double Vat
{
get
{
return 4;
/* // also tried this...
double shopVat = 19;
double vat = (_invoicePrice / (shopVat + 100)) * shopVat;
return Math.Round(vat, 2);
*/
}
}
the parsing does not work
and when I have this:
private double _invoicePrice = 0;
private double _vat = 0;
[XmlAttribute("invoicePrice")]
public double InvoicePrice{
get { return _invoicePrice; }
set { _invoicePrice = value; }
}
[XmlAttribute("vat")]
public double Vat
{
get { return _vat; }
set { _vat = value; }
}
the transformation works!
The property Vat
always had a value while debugging! with both options. But somehow the XslCompiledTransform
requires private variables or no read-only or something?
Can someone explain why option 2 works and the first does not..?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的答案是你需要一个二传手。
编辑2:设置器的原因
是否XML 序列化需要读取/写入属性吗?
http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx
END EDIT 2
现在,如果您尝试:
设置器是我唯一真正改变的一点。
您不需要将该值设置为任何值,但它确实允许 get 运行并返回您的增值税值。
编辑
建议允许该集合传入
shopVAT
以在GET
中使用。众所周知,这可以改变The simple answer is you need a setter.
EDIT 2: Reason for setter
Does XML Serialization Require Properties to be Read/Write?
http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx
END EDIT 2
For now if you try:
The setter is the only bit I have changed really.
You dont need to set the value to anything, but it does then allow the get to run and return your Vat value.
EDIT
a suggestion might be to allow the set to pass in that
shopVAT
for use in theGET
. As we all know this can change