【问题标题】:Console Application - how to read json file in xml file?控制台应用程序 - 如何读取 xml 文件中的 json 文件?
【发布时间】:2020-06-21 00:35:29
【问题描述】:

这是来自 c# 初学者的问题。我想输出在 xml 文件中有路径的 json 文件。我没有看到任何关于这个的教程。也许有人可以帮助我? 这是我的代码:

namespace Dev
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                XElement element = XElement.Load(@"D:\\TUTORIALS- VIDEO\\C#\\Tasks\\config.xml");
                List<Company> devices = new List<Company>();

                foreach (XElement item in element.Elements())
                {                
                    Console.WriteLine(item);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read.");
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();

        }

        public class Company
        {       
            public string jsonPathFileName { get; set; }

        }
    }
}

这似乎不起作用:-(

我的 xml: 这里的例子,我想输出company1.json。

<appConfig>
  <Media>
    <company>
        <name>Company 1</name>
        <id>1</id>
        <jsonPathFileName>./company1.json</jsonPathFileName>
        <apiUrl>http://demo.media.com/sp1</apiUrl>
        <username>monitor</username>
        <password>monitor</password>
    </company>
    <company>
        <name>Company 2</name>
        <id>1</id>
        <jsonPathFileName>./company2.json</jsonPathFileName>
        <apiUrl>http://demo.media.com/sp2</apiUrl>
        <username>monitor</username>
        <password>monitor</password>
    </company>
  </Media>
</appConfig>

【问题讨论】:

    标签: c# json xml console


    【解决方案1】:

    你可以使用LINQ,这样会容易得多:

    XElement element = XElement.Load(@"D:\\TUTORIALS- VIDEO\\C#\\Tasks\\config.xml");
    List<Company> devices = element.Element("Media").Elements("company").Select(c => new Company()
    {
        jsonPathFileName = c.Element("jsonPathFileName").Value,
        //to other properties
    }).ToList();
    
    foreach (Company item in devices)
    {
        //You can manupulate your path based on your files
        using (StreamReader r = new StreamReader(item.jsonPathFileName))
        {
            string json = r.ReadToEnd();
            dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(json);
        }
    }
    

    【讨论】:

    • 感谢回复,但没有读取 company1.json。
    • 嗯,我明白了,我已经更新了我的答案,请检查
    【解决方案2】:

    你可以试试下面的代码sn-p

    using System;
    using System.Xml;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System.Linq;
    
    public class Program
    {
        public static void Main()
        {
            string xml = @"<appConfig>
      <Media>
        <company>
            <name>Company 1</name>
            <id>1</id>
            <jsonPathFileName>./company1.json</jsonPathFileName>
            <apiUrl>http://demo.media.com/sp1</apiUrl>
            <username>monitor</username>
            <password>monitor</password>
        </company>
        <company>
            <name>Company 2</name>
            <id>1</id>
            <jsonPathFileName>./company2.json</jsonPathFileName>
            <apiUrl>http://demo.media.com/sp2</apiUrl>
            <username>monitor</username>
            <password>monitor</password>
        </company>
      </Media>
    </appConfig>";
    
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    
    string json = JsonConvert.SerializeXmlNode(doc);
    Console.WriteLine("->"+ json+ "<-");
            JObject o = JObject.Parse(json);
    
            var paths =
        from p in o["appConfig"]["Media"]["company"]
        select (string)p["jsonPathFileName"];
    
    foreach (var item in paths)
    {
        Console.WriteLine(item);
    }
    
        }
    }
    public class Company
            {       
                public string jsonPathFileName { get; set; }
    
            }
    

    【讨论】:

    • 感谢您的响应,但它在我的控制台“./company1.json”上的输出不是 json 文件中的对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2022-12-04
    • 2021-08-05
    相关资源
    最近更新 更多