-
-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to read XML file #389
Comments
Xml reading system still has to be improved, but for now, here is how you read XML file: dotnet new console -o TestXml
cd TestXml
dotnet add package Paillave.EtlNet.Core
dotnet add package Paillave.EtlNet.XmlFile here is the content of // See https://aka.ms/new-console-template for more information
using System.Text;
using Paillave.Etl.Core;
using Paillave.Etl.XmlFile;
using Paillave.Etl.XmlFile.Core;
var testXmlContent = @"<root>
<elt1 v1=""qwe""><v2>asd</v2></elt1>
<elt2 v3=""yxc""><v4>rtz</v4></elt2>
<elt1 v1=""mnb""><v2>poi</v2></elt1>
</root>";
var res = await StreamProcessRunner.CreateAndExecuteAsync("dummy", DefineProcess);
Console.WriteLine(res.Failed ? $"fail: {res.ErrorTraceEvent}" : "Success");
void DefineProcess(ISingleStream<string> contextStream)
{
var xmlNodes = contextStream
.Select("create in memory file with content for test", _ => FileValue.Create(new MemoryStream(Encoding.UTF8.GetBytes(testXmlContent)), "example.xml", "testContent"))
.CrossApplyXmlFile("parse xml", new MyXmlFileDefinition());
xmlNodes.XmlNodeOfType<Elt1Node>("only Etl1").Do("write elt1", i => Console.WriteLine($"Node type 1 : {i.V1} - {i.V2}"));
xmlNodes.XmlNodeOfType<Elt2Node>("only Etl2").Do("write elt2", i => Console.WriteLine($"Node type 2 : {i.V3} - {i.V4}"));
}
class MyXmlFileDefinition : XmlFileDefinition
{
public MyXmlFileDefinition()
{
this.AddNodeDefinition(XmlNodeDefinition.Create("elt1", "/root/elt1", i => new Elt1Node
{
V1 = i.ToXPathQuery<string>("/root/elt1/@v1"),
V2 = i.ToXPathQuery<string>("/root/elt1/v2"),
}));
this.AddNodeDefinition(XmlNodeDefinition.Create("elt2", "/root/elt2", i => new Elt1Node
{
V1 = i.ToXPathQuery<string>("/root/elt2/@v3"),
V2 = i.ToXPathQuery<string>("/root/elt2/v4"),
}));
}
}
class Elt1Node
{
public string V1 { get; set; }
public string V2 { get; set; }
}
class Elt2Node
{
public string V3 { get; set; }
public string V4 { get; set; }
} |
I am also having some issues with the XML Reader. I am trying to make it work without the need to create any specific class. Is this possible? What I have tried: `
` |
Hi Felipe. At the moment, you MUST return a concrete class as what you provide is not perceived as a factory but as a mapper (it is an expression, not a delegate). Moreover, this is the type of the returned element that will permit you to recognize the issued elements thanks to the operator |
Discussed in #388
Originally posted by LordBigDuck November 10, 2022
Hello, I have gone through the documentation and source code but didn't manage to write running code to read XML file. Could you provide some samples ?
The text was updated successfully, but these errors were encountered: