简体   繁体   中英

Convert XML file to JSON File using Blob trigger azure function

How to convert Xml file to Json file using Blob Trigger Azure function and .NET core 2 in Visual Studio.

Here is the code that worked for me considering below to be my XML file

<?xml version="1.0"?>
<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
  <Address Type="Shipping">
    <Name>Adam Murphy</Name>
    <Street>123 Maple Street</Street>
    <City>Mill Valley</City>
    <State>CA</State>
    <Zip>10999</Zip>
    <Country>Ireland</Country>
  </Address>
  <Address Type="Billing">
    <Name>Tai Yee</Name>
    <Street>8 Oak Avenue</Street>
    <City>Old Town</City>
    <State>PA</State>
    <Zip>95819</Zip>
    <Country>Ireland</Country>
  </Address>
  <DeliveryNotes />
</PurchaseOrder>

CODE:

using System;
using System.IO;
using System.Xml;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp16
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("samples/{name}", Connection = "")]Stream myBlob, string name, ILogger log)
        {
            XmlDocument doc = new XmlDocument();
            using (XmlReader reader = XmlReader.Create(myBlob))
            {
                doc.Load(reader);
            }
            string jsonText = JsonConvert.SerializeXmlNode(doc);
            Console.WriteLine(jsonText);

            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }
    }
}

RESULT:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM