小编joe*_*elc的帖子

XmlSerializer 用问号“?”替换非 ASCII 字符

我有一个非常适合 ASCII 的 XML 序列化程序,但是当遇到非 ASCII 字符时,它们会被替换为问号“?”。我相信我已经为 UTF8 正确配置了它,但不确定为什么要这样做。

XmlSerializer xmls = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
    XmlWriterSettings settings = new XmlWriterSettings();
    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
    ns.Add("", "");

    settings.Encoding = Encoding.UTF8;
    settings.Indent = true;
    settings.NewLineChars = "\n";
    settings.NewLineHandling = NewLineHandling.None;
    settings.NewLineOnAttributes = false;
    settings.ConformanceLevel = ConformanceLevel.Document;
    settings.OmitXmlDeclaration = true;

    using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
    {
        xmls.Serialize(writer, obj, ns);
    }

    string xml = Encoding.UTF8.GetString(ms.ToArray());

    // remove the BOM character at the beginning which screws up …
Run Code Online (Sandbox Code Playgroud)

c# xml xmlserializer

2
推荐指数
1
解决办法
2506
查看次数

3DES不会在相同的密文上为多次迭代产生一致的解密

我在我的代码中实现了一个3DES(TripleDES)解密例程,并注意到当提供的密文与前一次迭代相同时,它永远不会产生相同的明文.这似乎是不确定的,我确信这是我做错了.

我已经尝试过CBC(带有清零的IV),ECB,多种处理和清算组合.我似乎无法获得一致的输出.

(出于某种原因,下面"代码"和"输出"的代码格式不正确,抱歉)

using System;
using System.Security.Cryptography;
using System.Threading;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string key = "27F66D5244FF621EAA6F6120EDEB427F";
                string cipher = "C25C1D1197D31CAA87285D59A892047426D9182EC11353C051ADD6D0F072A6CB3436560B3071FC1FD11D9F7E74886742D9BEE0CFD1EA1064C213BB55278B2F12";

                Console.WriteLine("clear: " + byte_array_to_hex_string(
                    decrypt_3des(
                        hex_string_to_byte_array(cipher),
                        hex_string_to_byte_array(key)
                    ), true, true
                    ));

                Console.WriteLine("");
                Thread.Sleep(1000);
            }
        }

        static byte[] decrypt_3des(byte[] cipher, byte[] key)
        {
            if (cipher == null) return null;
            if (key == null) return null;
            int num_chunks = (cipher.Length) / 8;

            Console.WriteLine("Entering decrypt_3des");
            Console.WriteLine(" - cipher: " + byte_array_to_hex_string(cipher, …
Run Code Online (Sandbox Code Playgroud)

c# encryption 3des tripledes

2
推荐指数
1
解决办法
528
查看次数

禁用 AuthenticateAsServer 的证书验证似乎不起作用

我正在开发一个更大的 C# 套接字应用程序,它将通过 SSL 使用服务器和客户端身份验证。为了在我的本地机器上测试应用程序的其他部分,我需要禁用认证验证。我已经尝试按照其他几个地方的建议覆盖 ServerCertificateValidationNCallback,但我继续遇到异常。我正在使用使用 OpenSSL 创建的自签名证书进行测试。

我尝试过的带有解决方案的文章:

我得到的例外:

StackTrace:    at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest …
Run Code Online (Sandbox Code Playgroud)

c# ssl client-certificates

2
推荐指数
1
解决办法
2438
查看次数

带有System.Object对象的XmlSerializer

我似乎无法找到对此的明确回应,这让我发疯.XmlSerializer类似乎适用于任何已定义的内容,甚至适用于嵌套类.但是当其中一个类包含System.Object时,它就会呕吐.有没有人知道这方面的简单方法?

namespace test
{
    public class SomeList
    {
        public object obj;
    }

    public class Person
    {
        public string first;
        public string last;
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Creating object...");
                Person me = new Person();
                me.first = "MyFirst";
                me.last = "MyLast";

                Console.WriteLine("- Serialized: " + serialize_xml<Person>(me));
                Console.WriteLine("");
                Console.WriteLine("Creating object with embedded generic...");
                SomeList test = new SomeList();
                test.obj = me;
                Console.WriteLine("- Serialized: " + serialize_xml<SomeList>(test));

                Console.WriteLine("");
                Console.Write("Press ENTER to exit.");
                Console.ReadLine();
                return;
            }
            catch (Exception …
Run Code Online (Sandbox Code Playgroud)

.net c# serialization exception-handling xmlserializer

1
推荐指数
1
解决办法
3612
查看次数

加速字符串连接

我有一个程序,将循环写入一系列文件.使用提供给方法的对象中的参数构造文件名.

ANTS Performance Profiler说这是狗慢,我不知道为什么:

public string CreateFilename(MyObject obj)
{
    return "sometext-" + obj.Name + ".txt";
}
Run Code Online (Sandbox Code Playgroud)

有更高效的方式吗?该方法被击中了数千次,并且我不知道除了用于此目的的离散方法之外的好方法,因为输入对象不受我的控制并且经常改变.

c# string performance

1
推荐指数
1
解决办法
118
查看次数

当值可能存在于几个不同列之一时执行SUM

假设有一个简单的表:

CREATE TABLE [dbo].[foo](
    [foo_id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
        [val1_id] [int] NULL,
        [val1_amount] [int] NULL,
        [val2_id] [int] NULL,
        [val2_amount] [int] NULL,
        [val3_id] [int] NULL,
        [val3_amount] [int] NULL,
        [val4_id] [int] NULL
        [val4_amount] [int] NULL,
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)

还有一些其他表:

CREATE TABLE [dbo].[val](
    [val_id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
        [amount] [int] NOT NULL,
) ON [PRIMARY]
Run Code Online (Sandbox Code Playgroud)

应用程序的用户将从表val中选择一个条目,应用程序将以非确定的方式将val_id和amount放在val?_id和val?_amount中(是的,我知道这不好,但我有处理它).

假设val_id/amount可以存储在任何val?_id/val?_amount列中,是否有一种方法可以生成将由foo表中的val_id分组的输出和来自foo表的SUM值.请注意,如果val_id存储在val1_id中,则该数量将始终存储在该行内的val1_amount中,但相同的val_id可能会出现在另一行的val?_id中(但该数量将位于相应的列中)

sql-server sql-server-2008

0
推荐指数
1
解决办法
127
查看次数

将DataTable转换为JSON

我在这里回顾了答案:

但它们对我的特定用例没有帮助.我正在从SQL数据适配器检索DataTable,并希望将DataTable转换为List(这很容易),然后将List序列化为JSON(使用JSON.net,这很容易).

问题是我似乎必须使用List.好的,很好 - 所以我有这个代码:

DataTable result = GoMagicallyGatherSomeData();
List<DataRow> ret = new List<DataRow>();
if (result != null && result.Rows.Count > 0)
{
  foreach (DataRow curr in result.Rows)
  {
    ret.Add(curr);
  }
}
Run Code Online (Sandbox Code Playgroud)

要么

DataTable result = GoMagicallyGatherSomeData();
List<DataRow> ret = result.AsEnumerable().ToList();
Run Code Online (Sandbox Code Playgroud)

当我去序列化List时,它......不是我所期望的.

我想回来的是:

[  
   {  
      "TestId":1,
      "AccountId":1,
      "SomeString":"This is an updated test",
      "SomeTimestamp":"2016-01-01T00:00:00Z",
      "SomeDecimal":5.55
   },
   {  
      "TestId":3,
      "AccountId":1,
      "SomeString":"This is a third test",
      "SomeTimestamp":"2016-01-01T00:00:00Z",
      "SomeDecimal":5.55
   },
   { ... removed for brevity ... }
]
Run Code Online (Sandbox Code Playgroud)

而我实际得到的是: …

.net c# datatable ado.net json

0
推荐指数
1
解决办法
565
查看次数