将XML数据转换为JSON时 [英] While converting XML data to JSON

查看:143
本文介绍了将XML数据转换为JSON时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用c#将XML数据转换为JSON时,通过提供类中的数据,我得到了输出。我需要从文件夹中的文件中获取数据。可以请任何人帮忙吗

While converting XML data to JSON using c#, By providing the data within the class,I’m getting the output.I need to fetch data from file present in folder .Could anyone please help on this

using Newtonsoft.Json;
using System;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        
        private void Form1_Load(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(" <Student>\r\n    <Student_Name>Preethi</Student_Name>\r\n    <SSN>45679</SSN>\r\n    <Course>Electronics and communication</Course>\r\n    <Address>\r\n      <Line_1>#1, 6th cross</Line_1>\r\n      <Line_2>Victoria Layout</Line_2>\r\n      <City>Bangalore</City>\r\n      <Country>India</Country>\r\n    </Address>\r\n  </Student>\r\n</Root>");
            string jsonText = JsonConvert.SerializeXmlNode(doc);
            //Console.WriteLine(jsonText);
            MessageBox.Show(jsonText);
        }
    }
}




推荐答案

Hello ShreeKSD,

Hello ShreeKSD,

我不明白这个问题。 如果是因为示例中的xml无效,则只需删除末尾的无效Root元素。

I don't understand the question.  If it is because of the invalid xml in your example, just remove the invalid Root element at the end.

如果您询问如何从文件中读取,那么XmlDocument支持使用Load()方法加载文件的能力:

If you are asking how to read from a file then the XmlDocument supports the ability to load a file using the Load() method:


这篇关于将XML数据转换为JSON时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆