C
C#15mo ago
malkav

✅ My stringbuilder is skipping lines

Aside from a previous post I made: https://discord.com/channels/143867839282020352/1105409576586715166/1105409576586715166 I am trying to parse some strings lines, but the stringbuilder itself is already skipping lines and I'm not entirely sure why it skips those lines. The builder part (see the stream variable later):
StringBuilder builder = new();
using StreamReader reader = new(stream);
await reader.ReadLineAsync();

while (await reader.ReadLineAsync() is { } line && line != "---")
{
builder.AppendLine(await reader.ReadLineAsync());
}
StringBuilder builder = new();
using StreamReader reader = new(stream);
await reader.ReadLineAsync();

while (await reader.ReadLineAsync() is { } line && line != "---")
{
builder.AppendLine(await reader.ReadLineAsync());
}
The stream variable:
---
title: FP Config
hero_settings:
title: my title
subtitle: test meuk
button_text: mail
client_header:
title: clients
subtitle: of us
clients: []
services_header:
title: services
subtitle: services
expertise_header:
title: expertise
subtitle: stuff
about_header:
title: about
subtitle: about
about:
- leadmct: true
mct: true
mvp: true
title: bryce
subtitle: manager
image: url.to/image/path.png
partnerships_header:
title: partnerships
subtitle: partners
---
---
title: FP Config
hero_settings:
title: my title
subtitle: test meuk
button_text: mail
client_header:
title: clients
subtitle: of us
clients: []
services_header:
title: services
subtitle: services
expertise_header:
title: expertise
subtitle: stuff
about_header:
title: about
subtitle: about
about:
- leadmct: true
mct: true
mvp: true
title: bryce
subtitle: manager
image: url.to/image/path.png
partnerships_header:
title: partnerships
subtitle: partners
---
And what I get in the console is the following:
hero_settings:
subtitle: test meuk
client_header:
subtitle: of us
services_header:
subtitle: services
title: expertise
about_header:
subtitle: about
- leadmct: true
mvp: true
subtitle: manager
partnerships_header:
subtitle: partners
hero_settings:
subtitle: test meuk
client_header:
subtitle: of us
services_header:
subtitle: services
title: expertise
about_header:
subtitle: about
- leadmct: true
mvp: true
subtitle: manager
partnerships_header:
subtitle: partners
So my stringbuilder already fails 😅 I get more when I just do reader.ReadToEnd().Replace("---", "").Trim() but that will skip the body which could come after the last "---" part. So I need a way to include the body as well
11 Replies
MODiX
MODiX15mo ago
malkav#9104
So I have this .md to .yaml parser written, and a package that parses a yaml into a .NET object class, however it keeps telling me the properties cannot be found. For example, when I parse with the example class shown in https://dotnetfiddle.net/k1d6I2 my fiddle. The method I use for parsing is in there too. However, when I try to parse a markdown file:
---
title: FP Config
hero_settings:
title: my title
subtitle: test meuk
button_text: mail
client_header:
title: clients
subtitle: of us
clients: []
services_header:
title: services
subtitle: services
expertise_header:
title: expertise
subtitle: stuff
about_header:
title: about
subtitle: about
about:
- leadmct: true
mct: true
mvp: true
title: bryce
subtitle: manager
image: url.to/image/path.png
partnerships_header:
title: partnerships
subtitle: partners
---
---
title: FP Config
hero_settings:
title: my title
subtitle: test meuk
button_text: mail
client_header:
title: clients
subtitle: of us
clients: []
services_header:
title: services
subtitle: services
expertise_header:
title: expertise
subtitle: stuff
about_header:
title: about
subtitle: about
about:
- leadmct: true
mct: true
mvp: true
title: bryce
subtitle: manager
image: url.to/image/path.png
partnerships_header:
title: partnerships
subtitle: partners
---
I get the following error:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Property 'hero_settings' not found on type 'My.Namespace.Data.FrontPageSettings'.
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Property 'hero_settings' not found on type 'My.Namespace.Data.FrontPageSettings'.
Can anyone help me out here? Or is there an easier way to parse .md files into JSON instead? (not using a console application, like this git linkhttps://github.com/markjulmar/MarkdownToJson
React with ❌ to remove this embed.
TheRanger
TheRanger15mo ago
looks like it skips a line on every line it reads is this the whole code?
malkav
malkav15mo ago
Yes, I think I included a fiddle Oops, no Let me make a fiddle on the whole code
malkav
malkav15mo ago
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
TheRanger
TheRanger15mo ago
i can see why
malkav
malkav15mo ago
Why?
ero
ero15mo ago
lol yeah
TheRanger
TheRanger15mo ago
ur calling ReadLine twice in the while loop
ero
ero15mo ago
you AppendLine await reader.ReadLineAsync() instead of line
TheRanger
TheRanger15mo ago
and you never used the line variable change to
while (await reader.ReadLineAsync() is { } line && line != "---")
{
builder.AppendLine(line);
}
while (await reader.ReadLineAsync() is { } line && line != "---")
{
builder.AppendLine(line);
}
malkav
malkav15mo ago
catfacepalm ofc... I feel like an idiot now lol Yarp, that fixed everything 🤣 You guys are my heroes of today! Thanks!