动态生成 RDLC - 是什么阻止我使用 StringBuilder 来构建 XML

发布于 2024-07-20 05:33:49 字数 953 浏览 3 评论 0原文

我正在建立一份本地报告。 由于隐藏属性的一些限制,我需要动态生成报告。

我在此处找到了一些文档。

ReportViewer 控件需要一个流。

我真的不喜欢文档中使用的方法。 在我看来,构建 XmlDocument 的可读性不是很好。

有什么东西阻止我这样做吗

班级计划 { 静态无效主(字符串[]参数) { 生成报告(); }

    static void GenerateReport(){        
        StringBuilder reportXml = new StringBuilder();

        reportXml.Append("<Report>");
        reportXml.Append("<PageHeight>8.5in</PageHeight>");            
        reportXml.Append("</Report>");          

        XmlDocument xmlDocument = new XmlDocument();

        xmlDocument.LoadXml(reportXml.ToString());

        xmlDocument.Save(@"C:\test.xml");
        xmlDocument.Save(Console.Out);

        Console.ReadLine();
    }
}

I'm building a Local Report. Because of some limitations of the Hidden property, I need to dynamically generate the report.

I found some documentation here.

The ReportViewer Control needs a stream.

I don't really like the method that's used in the documentation. And building an XmlDocument isn't very readable imo.

Is there something stopping me from doing it like this

class Program
{
static void Main(string[] args)
{
GenerateReport();
}

    static void GenerateReport(){        
        StringBuilder reportXml = new StringBuilder();

        reportXml.Append("<Report>");
        reportXml.Append("<PageHeight>8.5in</PageHeight>");            
        reportXml.Append("</Report>");          

        XmlDocument xmlDocument = new XmlDocument();

        xmlDocument.LoadXml(reportXml.ToString());

        xmlDocument.Save(@"C:\test.xml");
        xmlDocument.Save(Console.Out);

        Console.ReadLine();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你げ笑在眉眼 2024-07-27 05:33:49

很简单:如果您使用该方法,处理器中的特殊操作会检测到错误的类用于生成该字符串,此时将调用正确性警察。

说真的,没有什么可以阻止你这样做。 事实上,在幕后,您可以打赌,更复杂的 XML 生成器中有一些代码可以执行非常类似的操作。 当您认真思考时,您会发现 XML 只是一个字符串,只要它格式良好,无论您如何构建它,该字符串都将是相同的。

其他类的优点是,当您想要构建更复杂的 XML 时,它们更简单、更灵活。


几乎有无限多个类可以生成 XML 或 XHTML。 寻找一个具有“流畅界面”的产品。 在类似 C++ 的语言中,它可能看起来像:

  XMLOutStream foo("filename.xml);
  foo.group("Top","attr=val")
     .group("Next")
     .line("Another", "attr=val") ;

为了生成,

 <Top>
   <Next attr="val">
      <Another attr="val" />
   </Next>
 </Top>

我在 这个问题中寻找类似的 HTML 内容

Simple: if you use that method, special operations in the processor detect that the Wrong Class ws used to generate that string, at which point the Correctness Police are called.

Seriously, there's nothing that keeps you from doing it exactly that way; in fact, under the covers you can bet there is some code in the more complicated XML generator that does something very much like that. When you get down to it, XML is just a string, and as long as it's well-formed, that string is going to be the same no matter how you build it.

The advantage of other classes is that they're simpler and more flexible when you want to build more complicated XML.


There are nearly infinitely many classes that can generate XML or XHTML. Look for one that has a "fluent interface". In a C++-like language it might look like:

  XMLOutStream foo("filename.xml);
  foo.group("Top","attr=val")
     .group("Next")
     .line("Another", "attr=val") ;

to generate

 <Top>
   <Next attr="val">
      <Another attr="val" />
   </Next>
 </Top>

I was looking for something similar for HTML in this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文