YAML auslesen und in Variablen speichern

  • VB.NET

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von VaporiZed.

    YAML auslesen und in Variablen speichern

    Moin Moin zusammen,

    leider hat die Forensuche mal wieder nicht das ergeben, was ich suche ;)
    Vielleicht kann mir hier jemand bei meinem Problemchen helfen.

    Ich versuche eine YAML.Datei einzulesen und die einzelnen Werte in diverse Variablen zu speichern.
    Nun hab ich über nuget bereits ein Paket installiert (YamlDotNet von Antoine Aubry)
    Leider finde ich keine Doku oder ansätze für VB.Net

    Hat jemand schon einmal sowas gemacht?

    Grüße SaMsOn
    Nein! Doch! OHH!
    @ISliceUrPanties das habe ich schon versucht,... Habe den erhaltenen Code auch schon versucht anzupassen, jedoch ohne Erfolg.
    Er findet nach dem Convert schon ein paar Namespaces nicht.

    Verwendet hab ich das hier
    github.com/aaubry/YamlDotNet

    C#
    Spoiler anzeigen

    C#-Quellcode

    1. using System.IO;
    2. using YamlDotNet.RepresentationModel;
    3. using YamlDotNet.Samples.Helpers;
    4. using Xunit.Abstractions;
    5. namespace YamlDotNet.Samples
    6. {
    7. public class LoadingAYamlStream
    8. {
    9. private readonly ITestOutputHelper output;
    10. public LoadingAYamlStream(ITestOutputHelper output)
    11. {
    12. this.output = output;
    13. }
    14. [Sample(
    15. DisplayName = "Loading a YAML Stream",
    16. Description = "Explains how to load YAML using the representation model."
    17. )]
    18. public void Main()
    19. {
    20. // Setup the input
    21. var input = new StringReader(Document);
    22. // Load the stream
    23. var yaml = new YamlStream();
    24. yaml.Load(input);
    25. // Examine the stream
    26. var mapping =
    27. (YamlMappingNode)yaml.Documents[0].RootNode;
    28. foreach (var entry in mapping.Children)
    29. {
    30. output.WriteLine(((YamlScalarNode)entry.Key).Value);
    31. }
    32. // List all the items
    33. var items = (YamlSequenceNode)mapping.Children[new YamlScalarNode("items")];
    34. foreach (YamlMappingNode item in items)
    35. {
    36. output.WriteLine(
    37. "{0}\t{1}",
    38. item.Children[new YamlScalarNode("part_no")],
    39. item.Children[new YamlScalarNode("descrip")]
    40. );
    41. }
    42. }
    43. private const string Document = @"---
    44. receipt: Oz-Ware Purchase Invoice
    45. date: 2007-08-06
    46. customer:
    47. given: Dorothy
    48. family: Gale
    49. items:
    50. - part_no: A4786
    51. descrip: Water Bucket (Filled)
    52. price: 1.47
    53. quantity: 4
    54. - part_no: E1628
    55. descrip: High Heeled ""Ruby"" Slippers
    56. price: 100.27
    57. quantity: 1
    58. bill-to: &id001
    59. street: |
    60. 123 Tornado Alley
    61. Suite 16
    62. city: East Westville
    63. state: KS
    64. ship-to: *id001
    65. specialDelivery: >
    66. Follow the Yellow Brick
    67. Road to the Emerald City.
    68. Pay no attention to the
    69. man behind the curtain.
    70. ...";
    71. }
    72. }



    VB.net
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports YamlDotNet.RepresentationModel
    3. Imports YamlDotNet.Samples.Helpers
    4. Imports Xunit.Abstractions
    5. Namespace YamlDotNet.Samples
    6. Public Class LoadingAYamlStream
    7. Private ReadOnly output As ITestOutputHelper
    8. Public Sub New(ByVal output As ITestOutputHelper)
    9. Me.output = output
    10. End Sub
    11. <Sample(DisplayName:="Loading a YAML Stream", Description:="Explains how to load YAML using the representation model.")>
    12. Public Sub Main()
    13. Dim input = New StringReader(Document)
    14. Dim yaml = New YamlStream()
    15. yaml.Load(input)
    16. Dim mapping = CType(yaml.Documents(0).RootNode, YamlMappingNode)
    17. For Each entry In mapping.Children
    18. output.WriteLine((CType(entry.Key, YamlScalarNode)).Value)
    19. Next
    20. Dim items = CType(mapping.Children(New YamlScalarNode("items")), YamlSequenceNode)
    21. For Each item As YamlMappingNode In items
    22. output.WriteLine("{0}" & vbTab & "{1}", item.Children(New YamlScalarNode("part_no")), item.Children(New YamlScalarNode("descrip")))
    23. Next
    24. End Sub
    25. Private Const Document As String = "---
    26. receipt: Oz-Ware Purchase Invoice
    27. date: 2007-08-06
    28. customer:
    29. given: Dorothy
    30. family: Gale
    31. items:
    32. - part_no: A4786
    33. descrip: Water Bucket (Filled)
    34. price: 1.47
    35. quantity: 4
    36. - part_no: E1628
    37. descrip: High Heeled ""Ruby"" Slippers
    38. price: 100.27
    39. quantity: 1
    40. bill-to: &id001
    41. street: |
    42. 123 Tornado Alley
    43. Suite 16
    44. city: East Westville
    45. state: KS
    46. ship-to: *id001
    47. specialDelivery: >
    48. Follow the Yellow Brick
    49. Road to the Emerald City.
    50. Pay no attention to the
    51. man behind the curtain.
    52. ..."
    53. End Class
    54. End Namespace


    Jedoch findet es den Namespace nicht "Dim yaml = New YamlStream()"

    Grüße SaMsOn
    Nein! Doch! OHH!
    Ich hab mir über Nuget das Package geholt und folgenden Code, der klappt.

    VB.NET-Quellcode

    1. Private Const Document As String = "---
    2. receipt: Oz-Ware Purchase Invoice
    3. date: 2007-08-06
    4. customer:
    5. given: Dorothy
    6. family: Gale
    7. items:
    8. - part_no: A4786
    9. descrip: Water Bucket (Filled)
    10. price: 1.47
    11. quantity: 4
    12. - part_no: E1628
    13. descrip: High Heeled ""Ruby"" Slippers
    14. price: 100.27
    15. quantity: 1
    16. bill-to: &id001
    17. street: |
    18. 123 Tornado Alley
    19. Suite 16
    20. city: East Westville
    21. state: KS
    22. ship-to: *id001
    23. specialDelivery: >
    24. Follow the Yellow Brick
    25. Road to the Emerald City.
    26. Pay no attention to the
    27. man behind the curtain.
    28. ..."
    29. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    30. Dim input = New StringReader(Document)
    31. Dim yaml = New YamlStream()
    32. yaml.Load(input)
    33. Dim mapping = CType(yaml.Documents(0).RootNode, YamlMappingNode)
    34. For Each entry In mapping.Children
    35. ListBox1.Items.Add((CType(entry.Key, YamlScalarNode)).Value)
    36. Next
    37. Dim items = CType(mapping.Children(New YamlScalarNode("items")), YamlSequenceNode)
    38. For Each item As YamlMappingNode In items
    39. ListBox1.Items.Add($"{item.Children(New YamlScalarNode("part_no"))}{Convert.ToChar(9)}{item.Children(New YamlScalarNode("descrip"))}")
    40. Next
    41. End Sub
    Bilder
    • Result.png

      2,85 kB, 337×244, 45 mal angesehen
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    @VaporiZed
    Danke fürs testen! Nachdem ich das Paket rausgeworfen hatte und neu installiert hab, ging es jetzt bei mir auch.
    Warum Wieso Weshalb, keine Ahnung.

    Ich bekomm es jedoch irgendwie nicht hin meine YAML Datei die ich auslesen möchte zu zerlegen.
    So schaut die YAML aus
    Spoiler anzeigen

    Quellcode

    1. ttlEncCert: 1440
    2. ttlEmailICCSN: 30
    3. performanceLog: false
    4. environmentPU: true
    5. konnektor:
    6. url: https://192.168.1.1/connector
    7. username:
    8. userpin:
    9. timeout: 60
    10. ldapurl: ldaps://192.168.1.1
    11. ntpServerAddress: 192.168.1.1
    12. ntpPeriod: 60
    13. dnsAddress: 192.168.1.1
    14. keyfile: ..\config\client.p12
    15. keypin: ABCDEFG123
    16. mail:
    17. clientKeyfile: ..\config\myclient.p12
    18. clientKeypin: ABCDEFG123
    19. serverKeyfile: ..\config\myserver.p12
    20. serverKeypin: ABCDEFG123
    21. timeoutClient: 300
    22. timeoutServer: 300
    23. smtpLogintype: LOGIN
    24. pop3Logintype: USER/PASS
    25. smtpPort: 465
    26. pop3Port: 995
    27. httpPort: 9443
    28. serviceName: AXV


    Es fliegt immer diese Exception:
    System.InvalidCastException: "Das Objekt des Typs "YamlDotNet.RepresentationModel.YamlMappingNode" kann nicht in Typ "YamlDotNet.RepresentationModel.YamlSequenceNode" umgewandelt werden."

    Den kompletten Code hab ich hier mal mit dazu.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2. ListBox1.Items.Clear()
    3. Dim input = New StringReader(Document)
    4. Dim yaml = New YamlStream()
    5. yaml.Load(input)
    6. Dim mapping = CType(yaml.Documents(0).RootNode, YamlMappingNode)
    7. For Each entry In mapping.Children
    8. ListBox1.Items.Add(CType(entry.Key, YamlScalarNode).Value)
    9. Next
    10. Dim items = CType(mapping.Children(New YamlScalarNode("mail")), YamlSequenceNode)
    11. For Each item As YamlMappingNode In items
    12. ListBox1.Items.Add($"{item.Children(New YamlScalarNode("url"))}{Convert.ToChar(9)}{item.Children(New YamlScalarNode("ldapurl"))}")
    13. Next
    14. End Sub


    Vom Aufbau her ist meine YAML gleich bzw. ähnlich der Demodaten, daher versteh ich nicht warum ich nicht auf die Subnodes zugreifen kann.
    Jemand eine Idee?
    Danke Stefan
    Nein! Doch! OHH!

    samson schrieb:

    System.InvalidCastException: "Das Objekt des Typs "YamlDotNet.RepresentationModel.YamlMappingNode" kann nicht in Typ "YamlDotNet.RepresentationModel.YamlSequenceNode" umgewandelt werden."
    Das klingt schon nach Import von zu vielen Namespaces. Entferne mal alle des Packages. Weil sonst importieren 2 Packs eine gleichgenannte Klasse und schon wird's Mus.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    Hast Du uns ein einfaches Beispielprojekt?
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    @VaporiZed
    Oberfläche ganz simpel zum Testen



    Hier mal der komplette Code:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports YamlDotNet.Core
    3. Imports YamlDotNet.RepresentationModel
    4. Public Class Form1
    5. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6. ListBox1.Items.Clear()
    7. Dim input = New StringReader(Document)
    8. Dim yaml = New YamlStream()
    9. yaml.Load(input)
    10. Dim mapping = CType(yaml.Documents(0).RootNode, YamlMappingNode)
    11. For Each entry In mapping.Children
    12. ListBox1.Items.Add(CType(entry.Key, YamlScalarNode).Value)
    13. Next
    14. Dim items = CType(mapping.Children(New YamlScalarNode("konnektor")), YamlSequenceNode)
    15. For Each item As YamlMappingNode In items
    16. ListBox1.Items.Add($"{item.Children(New YamlScalarNode("url"))}{Convert.ToChar(9)}{item.Children(New YamlScalarNode("ldapurl"))}")
    17. Next
    18. End Sub
    19. Private Const Document As String = "---
    20. ttlEncCert: 1440
    21. ttlEmailICCSN: 30
    22. performanceLog: false
    23. environmentPU: true
    24. konnektor:
    25. url: https://192.168.1.1/connector
    26. username:
    27. userpin:
    28. timeout: 60
    29. ldapurl: ldaps://192.168.1.1
    30. ntpServerAddress: 192.168.1.1
    31. ntpPeriod: 60
    32. dnsAddress: 192.168.1.1
    33. keyfile: ..\config\client.p12
    34. keypin: ABCDEFG123
    35. mail:
    36. clientKeyfile: ..\config\myclient.p12
    37. clientKeypin: ABCDEFG123
    38. serverKeyfile: ..\config\myserver.p12
    39. serverKeypin: ABCDEFG123
    40. timeoutClient: 300
    41. timeoutServer: 300
    42. smtpLogintype: LOGIN
    43. pop3Logintype: USER/PASS
    44. smtpPort: 465
    45. pop3Port: 995
    46. httpPort: 9443
    47. serviceName: AXV
    48. "
    49. End Class
    Nein! Doch! OHH!
    Sorry, Post#6 enthält ja schon meinen tl;dr-Fehler: »YamlMappingNode kann nicht in YamlSequenceNode umgewandelt werden.«
    Also: 2 Paar Stiefel.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.