C
C#3mo ago
kurumi

✅ XmlReader generic Mapper

Hello 👋 . I am trying to create generic mapper for my project, that is using XmlReader. All my XML files have same structure, i.e.:
<ROOT>
<OBJECT ID="52022696" OBJECTID="1456218" NAME="Abaya" TYPENAME="street" ISACTUAL="1" ISACTIVE="0"/>
...
</ROOT>
<ROOT>
<OBJECT ID="52022696" OBJECTID="1456218" NAME="Abaya" TYPENAME="street" ISACTUAL="1" ISACTIVE="0"/>
...
</ROOT>
For model:
public class AddressObject
{
public int Id { get; set; }

public int ObjectId { get; set; }

public string FullName { get; set; }
}
public class AddressObject
{
public int Id { get; set; }

public int ObjectId { get; set; }

public string FullName { get; set; }
}
So, I want to use Mapper this way:
public XmlReaderCopyHelper<AddressObject> Addresses =>
new XmlReaderCopyHelper<AddressObject>("OBJECT")
.WithCondition("ISACTUAL", src => src == "1") // ! only if ISACTUAL is "1" map, otherwise skip
.WithCondition("ISACTIVE", src => src == "1") // ! second condition
.Map("ID", ao => ao.Id)
.Map("OBJECTID", ao => ao.ObjectId)
.Map(..., ao => ao.FullName); // ! it should map to $"{TYPENAME} {NAME}"
public XmlReaderCopyHelper<AddressObject> Addresses =>
new XmlReaderCopyHelper<AddressObject>("OBJECT")
.WithCondition("ISACTUAL", src => src == "1") // ! only if ISACTUAL is "1" map, otherwise skip
.WithCondition("ISACTIVE", src => src == "1") // ! second condition
.Map("ID", ao => ao.Id)
.Map("OBJECTID", ao => ao.ObjectId)
.Map(..., ao => ao.FullName); // ! it should map to $"{TYPENAME} {NAME}"
13 Replies
kurumi
kurumi3mo ago
let me a sec to upload full code
kurumi
kurumi3mo ago
https://paste.mod.gg/ajnmxvtzfudh/0 So, the questions I have: 1) How can I improve performance? 2) How can I implement .Map(..., ao => ao.FullName); // ! it should map to $"{TYPENAME} {NAME}" P.S. by it should map to $"{TYPENAME} {NAME}" I mean that my mapper should find 2 XML attributes, i.e. TYPENAME="street" and NAME="Abaya" and for property FullName of provided generic class it will make "street Abaya" (final property should be created by lambda) Also, It will great to have functionality to map more than 2 attributes for property
BlazeBin - ajnmxvtzfudh
A tool for sharing your source code with the world!
kurumi
kurumi3mo ago
Before we start conversation, is it a good way to parse large files (few gbs) with this class, or should I create not generic parser for each model?
ffmpeg -i me -f null -
or should I create not generic parser for each model?
how can you have this doubt after having already created XmlReaderCopyHelper<>?
kurumi
kurumi3mo ago
I mean, I have deals with some reflection which can slower my app, but it is too sensitive for speed, caz it is 43 gb zipped xml files
ffmpeg -i me -f null -
you don't necessarily have to use reflection if you don't need to automate stuff, depends how many models you have also you are not caching anything in SetProperty
canton7
canton73mo ago
Maybe I'm missing the point but... Why not just use XmlSerializer? That does a bunch of code generation to give you pretty optimal deserialization
kurumi
kurumi3mo ago
It's slower then XmlReader, that's why
canton7
canton73mo ago
Have you read the generated code? It's literally generated code directly over XmlReader It's the same stuff you'd write by hand if you were writing a hand-optimised deserializer for a particular type
reflectronic
reflectronic3mo ago
whether XmlSerializer is slower than XmlReader doesn't matter. (of course it's slower, it's at least as slow as XmlReader, because that's what it uses to do the reading.) what matters is whether XmlSerializer is slower than your mapper
canton7
canton73mo ago
And unless you're doing code generation, or skipping out fairly basic sanity-checks, you're not going to get faster than XmlSerializer. Heck, hand-written code will struggle to get faster
kurumi
kurumi3mo ago
It makes sense. I thought XmlSerializer does not use XmlReader I'll try XmlSerializer then, thx!
canton7
canton73mo ago
Make sure you cache the generated XmlSerialiser
Want results from more Discord servers?
Add your server
More Posts
Disable Blazor NavLinkIs there any way to disable a NavLink in Blazor? While my application is doing some long calculationAsp.net Login and SignupI have form where user can login or sign up but I need help with form saving the credentials to data✅ C# + raylib + freebsdHi All, I'm running FreeBSD 14 Release, dotnet 8.0 compiled from Ports and Raylib 5.0 downloaded f✅ Setting Foreign key as Primary Key (EF Core)I want to set a foreign key as my primary key in a link table im using. How would I do this in EF CoWinform my button to reset my picturebox and fill it in with a new generated image is not workinghttps://hatebin.com/cslhmvmnya - my code Lines 121-124 is the methos to reset the picturebox but ImVS Project config questions1. Is it bad to enable "Place solution and project in the same folder"? It sounds neater to me but w(Blazor) I'm having trouble accessing a variable inside a parent component.I'm trying to access a simple bool in a parent component that shows when my side bar has been collap✅ There is already an open DataReader associated with this Command which must be closed firstI'm currently developing a Point Of Sale software in winforms c#. While working on cash management mC# Integration with MulesoftWe are looking to re-do an application in microservice style and biz req is pushing Mulesoft for thaVisual Studio opening search results in the search dock, not main windowKinda unrelated to C#, but since a few updates ago Visual Studio started behaving weirdly for me.