Home > Enterprise >  Create Blazor form dynamically from XML
Create Blazor form dynamically from XML

Time:09-29

Couldn't really find something on this online.

The XML file will be validated against our own schema. So all tags can be mapped.

My first thought is to create classes in code that represent the XML schema. So I can parse the xml to those classes and create the form from them. But then I am not sure how to determine the type of the inputfields since those can differ. And performance wise I don't think this is the best option.

So my question is does someone know another way to create create the Blazor components directly from the XML? Or should I do it as I've described?

Thanks!

CodePudding user response:

So I can parse the xml to those classes and create the form from them.

That seems the only way to do this. Blazor (razor) uses a declarative model, your code does not 'create' components but it instructs Blazor to do it.

But then I am not sure how to determine the type of the inputfields since those can differ.

You will have to Parse the XML down to that level and add the fields individually. The ChildContent approach can help with that.

And performance wise I don't think this is the best option.

The number of controls may affect performance, not how you create them. This is not a concern.

You can optimize a little by not writing out razor <MyComponent ... /> but go directly to builder.AddComponent(12, MyComponent) like code. Not as nice to write though.

  • Related