我有一些代码抛出XslTransformException.这是所需的行为(XSL包含一个xsl:message元素,其中@terminate设置为yes).
我试图在我的代码中捕获此异常,但无法找到包含此异常类的程序集,并且无法在MSDN中找到有关此异常的任何文档以了解合适的继承类(即避免使用我的catch块中的类Exception).
我已经引用了System.Xml和Sytem.Xml.Linq程序集,并具有以下using语句:
using System.Xml;
using System.Xml.Xsl;
Run Code Online (Sandbox Code Playgroud)
例外是在System.Xml.Xsl命名空间中; 即:
System.Xml.Xsl.XslTransformException
Run Code Online (Sandbox Code Playgroud)
知道我需要参考哪个程序集?
编辑: 根据要求,请在下面找到示例代码重现此异常:
using System;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
namespace StackOverflowDemo
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmsl = new XmlDocument();
xmsl.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" exclude-result-prefixes=\"msxsl\"><xsl:output method=\"xml\" indent=\"yes\"/><xsl:template match=\"@* | node()\"><xsl:message terminate=\"yes\">this should throw an exception</xsl:message><xsl:copy><xsl:apply-templates select=\"@* | node()\"/></xsl:copy></xsl:template></xsl:stylesheet>");
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(xmsl.CreateNavigator());
XmlDocument xml = new XmlDocument();
xml.LoadXml("<root />");
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
/* …Run Code Online (Sandbox Code Playgroud) 我需要生成多行带有GUID的xml标签:
<xmltag_10 value="{ZZZZZZZZ-ZZZZ-ZZZZ-ZZZZ-ZZZZZZZZZZZZ}"/>
<xmltag_11 value="{ZZZZZZZZ-ZZZZ-ZZZZ-ZZZZ-ZZZZZZZZZZZZ}"/>
Run Code Online (Sandbox Code Playgroud)
等等
我在循环中有此行,其中每次迭代都会生成$ guid,并且它会打印guid而不用大括号括起来
Write-Host ('<xmltag_{0} value="{1}"/>' -f $i,$guid)
<xmltag_10 value="ZZZZZZZZ-ZZZZ-ZZZZ-ZZZZ-ZZZZZZZZZZZZ"/>
Run Code Online (Sandbox Code Playgroud)
添加一组花括号,我得到
Write-Host ('<xmltag_{0} value="{{1}}"/>' -f $i,$guid)
<xmltag_10 value="{1}"/>
Run Code Online (Sandbox Code Playgroud)
如何逃避外花括号?我曾尝试使用`{{1}`}逃脱,但我得到了
Error formatting a string: Input string was not in a correct format..
Run Code Online (Sandbox Code Playgroud)
添加我的代码以进行复制和测试:
$i=10
while($i -lt 21)
{
$guid = ([guid]::NewGuid()).ToString().ToUpper();
Write-Host ('<xmltag_{0} value="{1}"/>' -f $i,$guid)
$i++
}
Run Code Online (Sandbox Code Playgroud) 我正在重构一些代码,试图使其更具自我记录性。当前代码对 OData 服务进行了查询,如下所示:
return context.MessageLog.Where
(
x =>
(
x.Status == MessageStatus.Success
|| x.Status == MessageStatus.Failure
)
&& x.Direction == MessageDirection.Inbound
&& x.ResponseDate == new DateTimeOffset(new DateTime(1900, 01, 01))
);
Run Code Online (Sandbox Code Playgroud)
我希望改变这一点以利用 Linq 表达式。
我可以将所有逻辑移动到单个表达式中并运行代码context.MessageLog.Where(MessageIsPendingResponse);。但是,我想为不同的条件创建表达式:(MessageIsProcessed即现在处于成功或失败状态)MessageIsInbound和ResponseNotYetSent(响应日期为空)。我可以将这些与多个 where 语句结合起来,如下所示:
return context.MessageLog
.Where(MessageLogExpression.MessageIsProcessed)
.Where(MessageLogExpression.MessageIsInbound)
.Where(MessageLogExpression.ResponseNotYetSent);
Run Code Online (Sandbox Code Playgroud)
(MessageLogExpression作为我用来包含这些预定义表达式的类)。
这是组合语句的最佳方式,还是先过滤错误字段的风险(例如,Linq 是否将所有条件组合成单个查询并允许查询引擎(在 SQL 术语中)确定最佳执行计划;或我们是否强制它首先过滤状态字段?
以上对于我们AND加入表达式的场景非常有用;但是我们怎么做OR呢?我认为有一些方法可以将这些结合起来,但找不到任何明显的东西。我怀疑这样的事情存在吗?
return context.MessageLog.Where(new OrExpression(MessageIsSuccess,MessageIsFailure));
Run Code Online (Sandbox Code Playgroud)
是否有一种在另一个表达式定义中组合表达式的好方法;例如像下面的代码(只有一个编译版本)?
public static Expression<Func<MessageLogRecord, bool>> MessageIsPendingResponse
{
get
{
Expression<Func<MessageLogRecord, bool>> …Run Code Online (Sandbox Code Playgroud) 我在一个单独的地方有一个类库和配置文件,我想使用它,所以我执行以下操作
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(configurationFile , true);
LogManager.ReconfigExistingLoggers();
Run Code Online (Sandbox Code Playgroud)
NLog 的配置是远程 app.config 的一部分,如下所示
<configuration>
<!-- ... -->
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<!--
Log in a separate thread, possibly queueing up to
5000 messages. When the queue overflows, discard any
extra messages
-->
<target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard"
layout="${machinename} - ${level} - ${longdate} - ${callsite} - ${threadid} - ${message} ${exception:format=tostring}">
<target xsi:type="File" fileName="C:\logs/${level}.txt" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
</configuration> …Run Code Online (Sandbox Code Playgroud) 给定代码,例如此 MRE:
function Get-One {1}
Update-TypeData -TypeName 'Demo' -MemberType 'ScriptProperty' -MemberName 'Test1' -Value {Get-Date}
Update-TypeData -TypeName 'Demo' -MemberType 'ScriptProperty' -MemberName 'Test2' -Value {Get-One}
$example = [PSCustomObject]@{PSTypeName = 'Demo'}
$example
Run Code Online (Sandbox Code Playgroud)
如果我调用它,pwsh -File '.\Demo.ps1'那么一切都会按您的预期工作/我得到以下输出:
Test1 Test2
----- -----
2021-04-17 21:35:55 1
Run Code Online (Sandbox Code Playgroud)
但是,如果我调用相同的命令来pwsh -Command '.\Demo.ps1'获取此命令(即 Test2 为空白);虽然我希望得到与上述相同的结果:
Test1 Test2
----- -----
2021-04-17 21:35:55
Run Code Online (Sandbox Code Playgroud)
即当我通过-Command参数调用时,ScriptProperty 无法访问脚本本身定义的 cmdlet/函数;虽然可以访问标准的(例如Get-Date)。
我认为这是一个错误;在 PWSH 和 PowerShell 中似乎只有相同的行为;这使得这种可能性小一点。
这是一个错误吗?或者我在理解中遗漏了什么。
如果我有一个继承自A的B和C类,是否有比使用StackFrame的GetFileName()更简单的东西(然后解析出ClassName.cs字符串)?
如果我使用this.GetType().Name,当代码在父类中执行时,它不会返回"A".
示例代码
namespace StackOverflow.Demos
{
class Program
{
public static void Main(string[] args)
{
B myClass = new C();
string containingClassName = myClass.GetContainingClassName();
Console.WriteLine(containingClassName); //should output StackOverflow.Demos.B
Console.ReadKey();
}
}
public class A { public A() { } }
public class B : A { public B() { } }
public class C : B { public C() { } }
}
Run Code Online (Sandbox Code Playgroud) 在创建新的部署包时(例如,根据http://msdn.microsoft.com/en-us/library/dd465323(v=vs.110).aspx),您需要提供包位置.
我想在这个文件名后加一个时间戳,这样我就可以通过浏览输出位置轻松访问旧版本了.
即我想指定一个这样的值:Packages\Test\MyProject {yyyy-mm-dd hh.mm.ss}.zip ...其中大括号中的值被当前日期/时间替换.
这可能通过原生视觉工作室吗?如果是这样,怎么办呢?
deployment timestamp web-deployment-project web-deployment visual-studio-2013
背景
我已经创建了一个包装器函数,Write-EventLog这样我就可以轻松地调用它,而不必每次都关心检查和创建事件日志/源.通过简单地添加一个-Force参数,我希望它创建日志,如果它不存在; 如果我不添加该参数,它应该正常运行(如果从我知道日志将存在的代码调用,这可以减少多次检查的开销).
我已经复制了可用参数列表,write-eventlog以便我可以使用包装器的全部功能.然而; 如果我不为这些参数中的某些参数提供值(例如RawData),则它们默认为null; 然后我最终尝试为此参数传递null; 这与不供应不同.我不想列出每个可能的参数组合迭代,并检查在调用适当的方法签名之前是否提供了这些值.
题
有没有办法只将值write-eventlog传递给传递这些参数的参数write-eventlog2?
码
cls
$myLog = 'Application'
$mySource = 'My PS Script'
$myEventId = 1
[System.Diagnostics.EventLogEntryType]$myEntryType = [System.Diagnostics.EventLogEntryType]::Error
$myMessage = 'This is a test message'
function Write-EventLog2
{
[cmdletbinding()]
param(
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$LogName
,
[Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$Source
,
[Parameter(Position=3,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Int32]$EventId
,
[Parameter(Position=4,Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[System.Diagnostics.EventLogEntryType]$EntryType = [System.Diagnostics.EventLogEntryType]::Information
,
[Parameter(Position=5,Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[String]$Message
,
[Parameter(Position=6,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
[Int16]$Category = 1
,
[Parameter(Position=7,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
[String]$ComputerName = $env:COMPUTERNAME
,
[Parameter(Position=8,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
[Byte[]]$RawData
,
[Parameter(Position=9,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
[switch]$Force
) …Run Code Online (Sandbox Code Playgroud) 在下面的示例中,我有一个IF语句来确定递归Demo调用时发送到哪些参数。如果$y是布尔值而不是开关,我可以简单地调用Demo -x $x -y $y; 但作为一个开关,这不是一个可行的选择。
function Demo {
[CmdletBinding()]
param (
[int]$x
,[switch]$y
)
process {
$x--
if ($x -gt 0) {
"$x - $($y.IsPresent)"
if($y.IsPresent) {
Demo -x $x -y
} else {
Demo -x $x
}
}
}
}
Demo 10 -y
Demo 10
Run Code Online (Sandbox Code Playgroud)
问题
上述是处理这种情况的正确方法,还是存在更清晰的选项?
我有一个PowerQuery,可以拉回两列; 一个Number(整数),另一个Text(一个字符串).我想创建一个连接这些列的自定义列.
看来这应该工作: ="(" & ([Value] as text) & ", " & [Description] & ")"
但这会返回错误 Expression.Error: We cannot convert the value 1 to type Text.
注意:这个问题与PowerQuery编辑器(即M语言)有关; 不是常规的Excel工作表函数.工作表的功能是实现我所追求的=concatenate("(", A2, ", ", B2, ")"),其中A2是Value,B2是Description.
c# ×4
powershell ×4
.net ×1
.net-3.5 ×1
deployment ×1
dll ×1
escaping ×1
excel ×1
exception ×1
guid ×1
linq ×1
nlog ×1
odata ×1
parameters ×1
powerquery ×1
timestamp ×1
xslt-1.0 ×1