ToString method returns value in quotes

Mar 14, 2010 at 6:51 AM

Why ToString() method returns values in quotes?

For example:

var yaml = YamlNode.FromYaml("src: dst")[0] as YamlMapping;
foreach (var pair in yaml)
{
    Console.WriteLine(pair.Key.ToString());
}
outputs
"src"

Coordinator
Mar 14, 2010 at 9:56 AM

Hi, Shidzo,

YamlNode.ToString() is offered mainly for debug usage.

Any YamlNode can be displayed as text by this method, even when it is not a YamlScalar
with a !!str tag. (Note that when the text expression becomes too long, it will be truncated.)

var yaml = YamlNode.FromYaml("src: dst")[0];
Console.WriteLine(yaml.ToString());
// => {"src": "dst"}

I thought quoting a text scalar value makes it easier to distinguish the node's content
during debug.

var yaml = YamlNode.FromYaml("src:dst")[0];
Console.WriteLine(yaml.ToString());
// => "src:dst"

If you want to know the scalar node's exact text expression, refer to YamlScalar.Value property.

Console.WriteLine(pair.Key.Value);
// => src

Mar 14, 2010 at 11:18 AM

Oh, I didn't guess to convert the received key object in my example to YamlScalar and thought that ToString() is the way to get the key value. You're right, I really wanted to get an exact value of the string expression. Sorry for bothering you :)

 

Coordinator
Mar 14, 2010 at 1:14 PM

If you didn't expect receiving the key as a YamlScalar object, you might become happier with looking
into the YamlSerializer class, instead of YamlNode class.

http://yamlserializer.codeplex.com/wikipage?title=YamlSerizlizer

YamlSerializer can serialize / deserialize C#'s native objects directly into / from YAML documents.

 

Mar 14, 2010 at 1:57 PM
Edited Mar 14, 2010 at 1:59 PM

Thank you, but YamlSerializer isn't quite suitable for my task, YamlNode is what I need.

P.S. You have misprints in some places - YamlSerizler, for example on home page and YamlSerializer page.