我需要比较两个xml文档.
假设以下每个XElement的负载来自Stream:
XElement actualElement = XElement.Load(actual);
XElement expectedElement = XElement.Load(expected);
Run Code Online (Sandbox Code Playgroud)
使用它,以下哪两个更好:
XNodeEqualityComparer comparer = new XNodeEqualityComparer();
comparer.Equals(actualElement, expectedElement);
Run Code Online (Sandbox Code Playgroud)
要么
XElement.DeepEquals(actualElement, expectedElement);
Run Code Online (Sandbox Code Playgroud)
我知道第二个选项更短,但我更感兴趣的是你在使用其中一个时是否获得任何速度提升或更好/更深的比较.比较本身需要比较两个xml文档之间的元素,属性和所有值.
我们使用BizTalk Server通过MSMQ发送消息.接收系统要求每条消息都将扩展属性设置为guid(作为字节数组).MSDN文档的MSMQMessage的扩展属性在这里(在.NET)和这里.
在.NET中设置扩展属性很简单:
const string messageContent = "Message content goes here";
var encodedMessageContent = new UTF8Encoding().GetBytes(messageContent);
// Create the message and set its properties:
var message = new System.Messaging.Message();
message.BodyStream = new System.IO.MemoryStream(encodedMessageContent);
message.Label = "AwesomeMessageLabel";
// Here is the key part:
message.Extension = System.Guid.NewGuid().ToByteArray();
// Bonus! Send the message to the awesome transactional queue:
const string queueUri = @"FormatName:Direct=OS:localhost\Private$\awesomeness";
using (var transaction = new System.Messaging.MessageQueueTransaction())
{
transaction.Begin();
using (var queue = new System.Messaging.MessageQueue(queueUri))
{
queue.Send(message, …Run Code Online (Sandbox Code Playgroud) 需要将来自可能具有重复键的对象的一组键/值对添加到字典中.只应将键的第一个不同实例(以及实例的值)添加到字典中.
下面是一个示例实现,首先出现,以便正常工作.
void Main()
{
Dictionary<long, DateTime> items = new Dictionary<long, DateTime>();
items = AllItems.Select(item =>
{
long value;
bool parseSuccess = long.TryParse(item.Key, out value);
return new { value = value, parseSuccess, item.Value };
})
.Where(parsed => parsed.parseSuccess && !items.ContainsKey(parsed.value))
.Select(parsed => new { parsed.value, parsed.Value })
.Distinct()
.ToDictionary(e => e.value, e => e.Value);
Console.WriteLine(string.Format("Distinct: {0}{1}Non-distinct: {2}",items.Count, Environment.NewLine, AllItems.Count));
}
public List<KeyValuePair<string, DateTime>> AllItems
{
get
{
List<KeyValuePair<string, DateTime>> toReturn = new List<KeyValuePair<string, DateTime>>();
for (int i = 1000; i …Run Code Online (Sandbox Code Playgroud)