带有对象和 XSLT 的 XslCompiledTransform

发布于 2024-10-21 06:58:28 字数 1064 浏览 1 评论 0原文

我已向类添加了一些属性和局部变量,并且希望使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2024-10-28 06:58:28

简单的答案是你需要一个二传手。

编辑2:设置器的原因

是否XML 序列化需要读取/写入属性吗?

http://msdn.microsoft.com/en-us/library/182eeyhh%28VS.85%29.aspx

END EDIT 2

现在,如果您尝试:

private double _invoicePrice = 0;

[XmlAttribute("invoicePrice")]
public double InvoicePrice{
    get { return _invoicePrice; }
    set { _invoicePrice = value; }
}

[XmlAttribute("vat")]
public double Vat
{
    get
    {
        double shopVat = 19;
        double vat = (_invoicePrice / (shopVat + 100)) * shopVat;
        return Math.Round(vat, 2);
    }
    set
    {
    }
}

设置器是我唯一真正改变的一点。

您不需要将该值设置为任何值,但它确实允许 get 运行并返回您的增值税值。

编辑
建议允许该集合传入 shopVAT 以在 GET 中使用。众所周知,这可以改变

private double _invoicePrice = 0;
private double _shopVat = 0;

[XmlAttribute("invoicePrice")]
public double InvoicePrice{
    get { return _invoicePrice; }
    set { _invoicePrice = value; }
}

[XmlAttribute("vat")]
public double Vat
{
    get
    {
        //double shopVat = 19;
        double vat = (_invoicePrice / (_shopVat + 100)) * _shopVat;
        return Math.Round(vat, 2);
    }
    set
    {
        _shopVat = value;
    }
}

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:

private double _invoicePrice = 0;

[XmlAttribute("invoicePrice")]
public double InvoicePrice{
    get { return _invoicePrice; }
    set { _invoicePrice = value; }
}

[XmlAttribute("vat")]
public double Vat
{
    get
    {
        double shopVat = 19;
        double vat = (_invoicePrice / (shopVat + 100)) * shopVat;
        return Math.Round(vat, 2);
    }
    set
    {
    }
}

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 the GET. As we all know this can change

private double _invoicePrice = 0;
private double _shopVat = 0;

[XmlAttribute("invoicePrice")]
public double InvoicePrice{
    get { return _invoicePrice; }
    set { _invoicePrice = value; }
}

[XmlAttribute("vat")]
public double Vat
{
    get
    {
        //double shopVat = 19;
        double vat = (_invoicePrice / (_shopVat + 100)) * _shopVat;
        return Math.Round(vat, 2);
    }
    set
    {
        _shopVat = value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文