base64引导到base64(base64 to guid to base64)
我目前正在研究MongoDb作为一种可能的数据库选项,而且我在处理Guid序列化时遇到了麻烦。 起初我认为这可能是C#驱动程序序列化中的一个错误,但现在我认为这更可能是我的天真假设。
为了帮助我将Bson base64表示法来回转换为Guids,我写了几个小小的PowerShell函数来帮助:
function base64toguid { param($str); $b = [System.Convert]::FromBase64String($str); $hex = ""; foreach ($x in $b) { $hex += $x.ToString("x2"); } $g = new-object -TypeName System.Guid -ArgumentList $hex; return $g; } function guidtobase64 { param($str); $g = new-object -TypeName System.Guid -ArgumentList $str; $b64 = [System.Convert]::ToBase64String($g.ToByteArray()); return $b64; }
我遇到的问题的一个示例:
:) guidtobase64("53E32701-9863-DE11-BD66-0015178A5E3C"); ASfjU2OYEd69ZgAVF4pePA== :) base64toguid("ASfjU2OYEd69ZgAVF4pePA=="); Guid ---- 0127e353-6398-11de-bd66-0015178a5e3c
从mongo外壳:
:) mongo MongoDB shell version: 1.6.5 connecting to: test > b = new BinData(3, "ASfjU2OYEd69ZgAVF4pePA=="); BinData(3,"ASfjU2OYEd69ZgAVF4pePA==") > b.hex(); 127e353639811debd66015178a5e3c >
正如你所看到的,我找回的Guid与我放入的不匹配。我的函数和hex()返回相同的东西。 如果您比较原始结果和结果:
53E32701-9863-DE11-BD66-0015178A5E3C
0127e353-6398-11de-bd66-0015178a5e3c你可以看到前三组十六进制对被颠倒过来,但最后两组不是。 这让我觉得有一些关于Guid.ToString(),我不明白。
任何人都可以教育我吗?
I'm currently researching MongoDb as a possible database option, and I'm having trouble dealing with Guid serialization. I thought at first maybe this was a bug in the C# driver's serialization, but now I think it's more likely a naive assumption on my part.
To help me convert the Bson base64 representations back and forth to Guids, I wrote a couple of little powershell functions to help:
function base64toguid { param($str); $b = [System.Convert]::FromBase64String($str); $hex = ""; foreach ($x in $b) { $hex += $x.ToString("x2"); } $g = new-object -TypeName System.Guid -ArgumentList $hex; return $g; } function guidtobase64 { param($str); $g = new-object -TypeName System.Guid -ArgumentList $str; $b64 = [System.Convert]::ToBase64String($g.ToByteArray()); return $b64; }
An example of the issue I'm having:
:) guidtobase64("53E32701-9863-DE11-BD66-0015178A5E3C"); ASfjU2OYEd69ZgAVF4pePA== :) base64toguid("ASfjU2OYEd69ZgAVF4pePA=="); Guid ---- 0127e353-6398-11de-bd66-0015178a5e3c
And from the mongo shell:
:) mongo MongoDB shell version: 1.6.5 connecting to: test > b = new BinData(3, "ASfjU2OYEd69ZgAVF4pePA=="); BinData(3,"ASfjU2OYEd69ZgAVF4pePA==") > b.hex(); 127e353639811debd66015178a5e3c >
So as you can see, the Guid I get back doesn't match what I put in. My function and hex() return the same thing. If you compare the original to the result:
53E32701-9863-DE11-BD66-0015178A5E3C
0127e353-6398-11de-bd66-0015178a5e3cYou can see that the first 3 sets of hex pairs are reversed, but the last 2 sets are not. This makes me think there is something about Guid.ToString() that I don't understand.
Can anyone educate me please?
原文:https://stackoverflow.com/questions/5172134
最满意答案
它是(行,字符)并从1开始(不是基于0)。
您应该检查
InnerException
以获得更精确的错误消息。 看看你的例子,它可能是这样的:"<TestSession> was not expected."
您还可以通过将
XmlRootAttribute
应用于您的类来更改序列化名称。It's (line, character) and begins at 1 (not 0 based).
You should examine the
InnerException
to get a more precise error message. Looking at your example, it could be something like:"<TestSession> was not expected."
You can also change the serialized name by applying
XmlRootAttribute
to your class.
相关问答
更多-
XmlSerializer将在它到达套接字末尾时读取流(即将等待)。 没有必要缓冲它。 但是,常见的错误不是在发送端清除输出流(使得另一端的解串器永远等待)。 您应该确保在调用Serialize()后刷新它。 XmlSerializer will read the stream as it arrives at your end of the socket (i.e. will wait). There is no need to buffer it. However a common error is n ...
-
使用XmlSerializer.Deserialize处理FormatExceptions(Handling FormatExceptions using XmlSerializer.Deserialize)[2023-11-10]
如果第三方模式将该字段定义为日期时间,则应该始终包含有效的日期时间值,除非出现问题。 在这种情况下,您可能会考虑将其反序列化为字符串 public class Book { [XmlElement("release_date")] public string ReleaseDate { get; set; } public DateTime? GetReleaseDate { get { // parse ReleaseD ... -
.net:如何调试XmlSerializer.Deserialize错误?(.net: how do debug XmlSerializer.Deserialize errors?)[2023-02-25]
它是(行,字符)并从1开始(不是基于0)。 您应该检查InnerException以获得更精确的错误消息。 看看你的例子,它可能是这样的: "was not expected." 您还可以通过将XmlRootAttribute应用于您的类来更改序列化名称。 It's (line, character) and begins at 1 (not 0 based). You should examine the InnerException to get a more precis ... -
XmlSerializer.Deserialize为子类(不是数组)(XmlSerializer.Deserialize for a sub class (not an array))[2023-10-12]
至于您的第一个问题, XmlSerializer可以处理简单的层次结构,因此在您的示例中, XmlAttributeOverrides不是必需的: [Serializable] [XmlRoot("custom")] public class Custom { [XmlElement("investment")] public string Investment { get; set; } [XmlElement("offth ... -
是的,当输入不包含预期的XML时, Deserialize可以返回null。 这在XML命名空间混淆时经常出现。 如果输入包含具有期望名称的根元素,但在不同的名称空间中,则将返回null。 在处理ASMX Web服务或Web引用时,通常会看到这种情况,特别是针对RPC式服务的Web引用,其中消息根据消息的XSD type进行描述,而不是根据element 。 Yes, Deserialize can return null when the input does not contain the XML th ...
-
在没有查看所有代码或XML的情况下,我认为问题在于ReadSubtree()只会读取匹配的第一个元素。 所以,正如你所说它似乎工作得很好,除了数组只包含一个元素。 one元素是否是XML文档中的第一个匹配元素? 如果是这样,我认为这是你的问题。 以下MSDN文档链接中的示例显示了ReadSubtree()方法的工作原理。 你会注意到他们首先读取一个节点,然后跳过,然后ReadSubtree()方法将读取他们的示例xml上的第二个节点,仅此而已。 MSDN上的ReadSubtree() 希望这可以帮助你! W ...
-
尝试这个..... Usings ..... using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; 类..... [XmlRoot(ElementName = "currency")] public class Currency { [XmlElement(ElementName = " ...
-
实现默认值是你的工作 ; [DefaultValue]只是说“这是默认值,你不需要担心这个” - 它不适用它。 这不仅适用于XmlSerializer ,而且适用于[DefaultValue]所属的核心System.ComponentModel API(它驱动PropertyGrid的粗体/非粗体等内容) 基本上: public class DefaultValueTestClass { public DefaultValueTestClass() { Foo = 10000 ...
-
XmlSerializer.Deserialize - 忽略不必要的元素?(XmlSerializer.Deserialize - ignore unnecessary elements?)[2023-07-22]
我认为没有办法做到这一点。 您必须修复架构或手动修改由xsd.exe生成的代码以允许对XML进行反序列化。 您还可以尝试在Visual Studio或任何其他具有模式支持的XML编辑器中打开XML文档+模式,以修复模式或XML文档。 I don't think there is an option to do this. You either have to fix the schema or manually modify the code generated by xsd.exe to allow th ... -
只有当您想通过XmlInclude属性自动反序列化任何/所有派生类时,才必须为基类型创建序列化程序。 您的代码无效,因为您开始创建XmlSerializer serializer = new XmlSerializer(typeof(D)); 这是子类型的序列化程序,没有基类的上下文。 稍后当您尝试使用类型B的Serializer反序列化此子xml时,它会失败,因为生成的序列化Xml没有基类型的上下文。 您必须创建类型B的Serializer,以便生成的Xml具有属性xsi:type ,该属性指示XmlSe ...