我有C#代码对OData Web服务执行POST,这会导致将记录插入到数据库中。如果存在异常,例如主键冲突,我想捕获并记录错误消息。但是,该消息包含在XML中。这是我的异常处理程序:
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
string responseText = string.Empty;
using (Stream responseStream = ((HttpWebResponse)ex.Response).GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
responseText = streamReader.ReadToEnd();
}
}
Debug.WriteLine(responseText);
return responseText;
}
else
{
Debug.WriteLine(ex.Message.ToString());
return ex.Message.ToString();
}
}
以下是我在responseText中的内容:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<code></code>
<message xml:lang="en-US">An error occurred while processing this request.</message>
<innererror>
<message>An error occurred while updating the entries. See the inner exception for details.</message>
<type>System.Data.UpdateException</type>
<stacktrace> at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Services.Providers.ObjectContextServiceProvider.SaveChanges()
at System.Data.Services.DataService 1.HandleNonBatchRequest(RequestDescription description)
at System.Data.Services.DataService 1.HandleRequest()</stacktrace>
<internalexception>
<message>Violation of PRIMARY KEY constraint 'PK_Customer_CustomerId'. Cannot insert duplicate key in object 'VertexBilling.Customer'.
The statement has been terminated.</message>
<type>System.Data.SqlClient.SqlException</type>
<stacktrace> at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary 2 identifierValues, List 1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)</stacktrace>
</internalexception>
</innererror>
</error>
我需要从innererror中的internalexception中获取消息。最简单的方法是什么?
答案 0 :(得分:1)
您应该使用XML工具包,您已经可以在.net中使用它了,它可以在将xml视为文本时消除歧义问题(这是一个等待发生的噩梦,在上面的xml中你有两个消息节点)
或者你可以构建一小段代码,它将innerException提取/反序列化为Exception实例,并可以将其重用于其他调用。
如果你不知道如何做到这一点,咆哮,我会砰的一声。
答案 1 :(得分:0)
最简单方式可能是使用如下所示的正则表达式来提取"<message>"
和"</message>"
之间的字符串:
<message>(.+)</message>
这不是非常强大,并且它不符合XML的精神,但它应该有效,除非XML变化很大。如果你还没有在你的应用程序中使用XML工具,那么你不一定要提升它们只是为了提取这个字符串。