C
C#9mo 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
kurumiOP9mo ago
let me a sec to upload full code
kurumi
kurumiOP9mo 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
kurumiOP9mo 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?
HtmlCompiler
HtmlCompiler9mo ago
or should I create not generic parser for each model?
how can you have this doubt after having already created XmlReaderCopyHelper<>?
kurumi
kurumiOP9mo 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
HtmlCompiler
HtmlCompiler9mo ago
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
canton79mo 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
kurumiOP9mo ago
It's slower then XmlReader, that's why
canton7
canton79mo 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
reflectronic9mo 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
canton79mo 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
kurumiOP9mo ago
It makes sense. I thought XmlSerializer does not use XmlReader I'll try XmlSerializer then, thx!
canton7
canton79mo ago
Make sure you cache the generated XmlSerialiser
Want results from more Discord servers?
Add your server