Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse yaml file VB,net #940

Closed
316John opened this issue Jul 11, 2024 · 9 comments
Closed

parse yaml file VB,net #940

316John opened this issue Jul 11, 2024 · 9 comments

Comments

@316John
Copy link

316John commented Jul 11, 2024

Hello, I'm trying to view all the contents of a yaml file but I'm having trouble converting the sample code from C# to VB.net. Visual Studio gives an error in this code at the "For each key" line stating that 'key' is not declared but I'm not sure what I need to declare it to. Thanks for your help!

Dim YamlData As String = File.ReadAllText(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "" & "filter.yaml")
Dim inputYaml = New StringReader(YamlData)
Dim yamled = New YamlStream()
yamled.Load(inputYaml)

' Examine the stream
Dim mappings As YamlMappingNode = CType(yamled.Documents(0).RootNode, YamlMappingNode)

For Each key In mappings.Children
Console.WriteLine(CType(entry.Key, YamlScalarNode).Value)
Next

@EdwardCooke
Copy link
Collaborator

EdwardCooke commented Jul 12, 2024

The children property is of type ordereddictionary<yamlnode, yamlnode> so when you do a for each on that I believe the type will be of keyvaluepair<yamlnode, yamlnode> which is what you should declare your key variable as. I’m not a computer right now and the one I have doesn’t have the Visual Basic workload so I can’t piece together a full example in vb for about a week if necessary.

public IOrderedDictionary<YamlNode, YamlNode> Children

@316John
Copy link
Author

316John commented Jul 12, 2024

I should have included a screenshot for clarity on what needs declared. Here you go: entry

I'm trying to read the contents of a yaml file, edit, and save it. Sadly, I'm working with VB.net but all the samples are in C#. Thanks for your help!

@EdwardCooke
Copy link
Collaborator

This is working code that dumps out the key

        Dim YamlData As String = "hello: world"
        Dim inputYaml = New StringReader(YamlData)
        Dim yamled = New YamlStream()
        yamled.Load(inputYaml)

        ' Examine the stream
        Dim mappings As YamlMappingNode = CType(yamled.Documents(0).RootNode, YamlMappingNode)

        For Each entry In mappings.Children
            Console.WriteLine(CType(entry.Key, YamlScalarNode).Value)
        Next

Results in

hello

@316John
Copy link
Author

316John commented Jul 14, 2024

Thanks for the help Edward! That's what I have but "entry" is still not defined correctly. I have yamldotnet installed from GitHub and have it declared as an import but VS has "entry" flagged as undefined. I'm missing something.

@EdwardCooke
Copy link
Collaborator

That was a copy paste of the entire application. Can you put up a repo with the minimum amount of code required to reproduce it?

@316John
Copy link
Author

316John commented Jul 15, 2024

I pasted your code into a new application and loaded yaldotnet 16.0 and entry did not trigger an error. That was helpful and lead me to figure out the problem. In the application I am using yamldotnet, I have Option Explicit On which means that all variables must be declared. So the "For Each entry In" is not declared explicitly. When I turn Option Explicit Off, the error goes away. This proves what I thought was wrong originally that the "entry" needs to be declared. My "For Each" syntax looks like "For Each row as DataRow in test.datatable.rows". I just don't know how to declare entry.

@EdwardCooke
Copy link
Collaborator

The data type should be KeyValuePair<YamlNode, YamlNode>

@316John
Copy link
Author

316John commented Jul 16, 2024

OK, that worked. Here's how it looks in VB.net

For Each entry As KeyValuePair(Of YamlNode, YamlNode) In mappings.Children
Console.WriteLine(CType(entry.Key, YamlScalarNode).Value)
Next

@316John
Copy link
Author

316John commented Jul 16, 2024

Thanks for your help! I have another question. I am able to view the content of a yaml file but how do I take the content I write to the console and save it to another yaml file to make a duplicate? Here's my code to view the content:

Dim ContentYaml As String = File.ReadAllText("C:\original.yaml")
Dim Yamlstr As New YamlStream()
Dim stringBuilder As New StringBuilder()
Dim serializer As New Serializer()
serializer.Serialize(Console.Out, ContentYaml)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants