Home > front end >  How to export/generate XML file with METADATA in asp.net core C#
How to export/generate XML file with METADATA in asp.net core C#

Time:12-07

I need to get data from database, massage it into list of objects, then finally generate it into xml with those METADATA tag (expected result), I think those DATA PACKET, METADATA, etc.. tag is not manually written it, may there is some library from asp.net core C# able to generate it automatically when serialized the data into xml file?

Here is the code I tried out, it didn't turns out to expected result as I need it.

    public class Book
    {
        public string title;
        public string author;
        public string publisher;
        public double price;
    }
  
    public void GenerateXML()
    {
       var overview = new List<Book>{
            new Book()
            {
                title = "This is a book",
                author = "Somebody wrote it",
                publisher = "Someone published it",
                price = 999.99
            },
            new Book()
            {
                title = "This is a book2",
                author = "Somebody wrote it2",
                publisher = "Someone published it2",
                price = 101010
            }
        };

        System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(Book));

        var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)   "//myXmFile.xml";
        System.IO.FileStream file = System.IO.File.Create(path);

        foreach (var item in overview)
        {
            writer.Serialize(file, item);

        }

        file.Close();
    }

it turns out

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <title>
        This is a book
    </title>
    <author>
        Somebody wrote it
    </author>
    <publisher>
        Someone published it
    </publisher>
    <price>
        999.99
    </price>
</Book>
<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <title>
        This is a book2
    </title>
    <author>
        Somebody wrote it2
    </author>
    <publisher>
        Someone published it2
    </publisher>
    <price>
        101010
    </price>
</Book>

Expected result:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<DATAPACKET Version="2.0">
    <METADATA>
            <FIELDS>
                <FIELD attrname="title" fieldtype="string"/>
                <FIELD attrname="author" fieldtype="string"/>
                <FIELD attrname="publisher" fieldtype="string"/>
                <FIELD attrname="price" fieldtype="r8"/>
            </FIELDS>
        <PARAMS/>
    </METADATA>
    
    <ROWDATA>
        <ROW title="This is a book" author="Somebody wrote it" publisher="Someone published it" price="999.99" />
        <ROW title="This is a book2" author="Somebody wrote it2" publisher="Someone published it2" price="101010" />
    </ROWDATA>
</DATAPACKET>

Is there any library from asp.net core C# able to generate it into expected result?

CodePudding user response:

Here is the code. I generated the class using Generated XML

  • Related