如何从 Visual Studio 2003 中的调试器检查 XML 流

发布于 2024-07-19 05:14:49 字数 782 浏览 6 评论 0原文

我必须编辑 XSLT 样式表,但我很盲目,因为 XML 输入仅短暂存在于一堆流中。 我可以调试代码,但无法弄清楚如何将流的内容转换为我可以查看的文本(并在编辑它们时手动运行 XSLT)。

该代码是一个大型旧遗留系统的一部分,如果绝对必要,我可以在调试环境中修改它,但它运行在连接到一堆 MSMQ 的 Windows 服务中。 因此,出于各种原因,我宁愿能够使用调试器来查看 XML,而不必先更改代码。

代码更加简化,如下所示:(C# - 但请记住它是 VS 2003 中的 .net 1.1。)

该函数将 XML 作为流获取,然后将其馈送到某种 XSLT 转换对象中。 我尝试在监视窗口和即时窗口中查看 writer 和 xmlStream 对象,但不太明白如何查看实际的 XML。

private MemoryStream GetXml()
{
    MemoryStream xmlStream;
    xmlStream = new MemoryStream();
    XmlWriter writer = new XmlTextWriter(xmlStream, Encoding.UTF8);
    writer.WriteStartDocument();
    //etc etc...
    writer.WriteEndDocument();
    writer.Flush();
    xmlStream.Position = 0;
    return xmlStream; //Goes off to XSLT transform thingy!
}

非常感谢所有帮助。

I've got to edit an XSLT stylesheet, but I'm flying blind because the XML input only exists fleetingly in a bunch of streams. I can debug into the code, but can't figure out how to get the contents of the streams out into text I can look at (and run through the XSLT manually while I'm editing them).

The code is part of a big old legacy system, I can modify it in a debug environment if absolutely necessary, but it runs in a windows service connected up to a bunch of MSMQs. So for various reasons I'd rather be able to use the debugger to see the XML without having to change the code first.

Code much simplified, is something like this: (C# - but remember it's .net 1.1 in VS 2003.)

This is the function that gets the XML as a stream, which is then fed into some sort of XSLT transform object. I've tried looking at the writer and xmlStream objects in the watch windows and the immediate window, but can't quite fathom how to see the actual XML.

private MemoryStream GetXml()
{
    MemoryStream xmlStream;
    xmlStream = new MemoryStream();
    XmlWriter writer = new XmlTextWriter(xmlStream, Encoding.UTF8);
    writer.WriteStartDocument();
    //etc etc...
    writer.WriteEndDocument();
    writer.Flush();
    xmlStream.Position = 0;
    return xmlStream; //Goes off to XSLT transform thingy!
}

All help much appreciated.

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

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

发布评论

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

评论(2

假面具 2024-07-26 05:14:49

在 MemoryStream 准备就绪后,您可以简单地将此表达式添加到监视窗口:

(new StreamReader(xmlStream)).ReadToEnd();

监视表达式不需要是简单的变量值。 它们可以是复杂的表达式,但它们会产生副作用。 正如您所注意到的,这将中断执行,因为流内容将被完全读出。 如果需要重新开始执行,您可以在中断后使用另一个表达式重新创建流。

在使用流调试代码时,这种情况经常出现,因此我在简单、独立的任务中避免使用它们。 不幸的是,对于大型系统,提前知道是否应该使代码面向流并不总是容易的,因为这在很大程度上取决于它将如何使用。 然而,我认为在许多情况下使用流是一种过早的优化。

You could simply add this expression to your watch window after the MemoryStream is ready:

(new StreamReader(xmlStream)).ReadToEnd();

Watch expressions don't need to be simple variable values. They can be complex expressions, but they will have side-effects. As you've noted, this will interrupt execution, since the stream contents will be read out completely. You could recreate the stream after the interruption with another expression, if you need to re-start execution.

This situation arises frequently when debuging code with streams, so I avoid them for simple, self-contained tasks. Unfortunately, for large systems, it's not always easy to know in advance whether you should make your code stream-oriented or not, since it depends greatly on how it will be used. I consider the use of streams to be a premature optimization in many cases, however.

陈甜 2024-07-26 05:14:49

好吧,我没有成功地使用调试器而不修改代码。 我添加了以下代码片段,它让我可以放置断点或使用 debugview。

private MemoryStream GetXml()
{
    MemoryStream xmlStream;
    xmlStream = new MemoryStream();
    XmlWriter writer = new XmlTextWriter(xmlStream, Encoding.UTF8);
    writer.WriteStartDocument();
    //etc etc...
    writer.WriteEndDocument();
    writer.Flush();
    xmlStream.Position = 0;

    #if DEBUG
    string temp;
    StreamReader st=new StreamReader(xmlStream);
    temp=st.ReadToEnd();
    Debug.WriteLine(temp);
    #endif

    return xmlStream; //Goes off to XSLT transform thingy!
}

我仍然更愿意以某种方式简单地查看调试器中的 xmlstream 对象,即使它会破坏执行流程,但与此同时,这是我管理过的最好的。

OK, I've not succeeded in using the debugger without modifying the code. I added in the following snippet, which lets me either put a breakpoint in or use debugview.

private MemoryStream GetXml()
{
    MemoryStream xmlStream;
    xmlStream = new MemoryStream();
    XmlWriter writer = new XmlTextWriter(xmlStream, Encoding.UTF8);
    writer.WriteStartDocument();
    //etc etc...
    writer.WriteEndDocument();
    writer.Flush();
    xmlStream.Position = 0;

    #if DEBUG
    string temp;
    StreamReader st=new StreamReader(xmlStream);
    temp=st.ReadToEnd();
    Debug.WriteLine(temp);
    #endif

    return xmlStream; //Goes off to XSLT transform thingy!
}

I'd still prefer to simply look at the xmlstream object in the debugger somehow, even if it disrupts the flow of execution, but in the meantime this is the best I've managed.

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