首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#在listBox中列出的项在循环中序列化为json

C#在listBox中列出的项在循环中序列化为json
EN

Stack Overflow用户
提问于 2022-03-24 15:44:09
回答 1查看 69关注 0票数 0

所以我在这方面是个新手,我有个问题。

我想要加载一个XML文件并从中获取一个属性,这是可行的。现在我想把它序列化成一个JSON格式的文件,但是现在我被困住了,而且我的头变得疯狂了,…

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using Newtonsoft.Json;
using Formatting = Newtonsoft.Json.Formatting;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {     
        public Form1()
        {
            InitializeComponent();
            Loadtype();
            //Load XML
            XmlDocument doc = new XmlDocument();
            doc.Load("types.xml");

            foreach (XmlNode node in doc.DocumentElement)
            {
                string value = listBox.Text.ToString();
                string name = node.Attributes[0].Value;
                listBox.Items.Add(new type(name));
            }
            
        }      

        private void button1_Click(object sender, EventArgs e)
        {

         
            Item test = new Item()
            {               
              
                m_Version = 9,
                DisplayName = "Trader",
                Icon = "Trader",
                Color = "ef5252FF",
                InitStockPercent = 100,
                
                Items = new List<Root>()
                {

                    new Root { ClassName = "", MaxPriceThreshold = 5000, MinPriceThreshold = 5000, SellPricePercent = -1, MaxStockThreshold = 1, MinStockThreshold = 1, QuantityPercent = -1 },
   
                }

            };
                string json = JsonConvert.SerializeObject(test, Formatting.Indented);
                Console.WriteLine(json);
                Console.Read();
            



            foreach (var line in listBox.Items)
            { 
                
            }

            const string sPath = "save.json";
            System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(sPath);
            SaveFile.Close();
            MessageBox.Show("Expansion Trader Market .json saved!");
        }

        public class Item
        {

            public int m_Version { get; set; }
            public string DisplayName { get; set; }
            public string Icon { get; set; }
            public string Color { get; set; }
            public int InitStockPercent { get; set; }
            public List<Root> Items { get; set; } 

        }

        public class Root
        {
            public string ClassName { get; set; }
            public int MaxPriceThreshold { get; set; }
            public int MinPriceThreshold { get; set; }
            public int SellPricePercent { get; set; }
            public int MaxStockThreshold { get; set; }
            public int MinStockThreshold { get; set; }
            public int QuantityPercent { get; set; }
        }

        private void Loadtype()
        {


        }

        private void listBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

输出(除了一件事外,这很好)

代码语言:javascript
复制
{
  "m_Version": 9,
  "DisplayName": "Trader",
  "Icon": "Trader",
  "Color": "ef5252FF",
  "InitStockPercent": 100,
  "Items": [
    {
      "ClassName": "",
      "MaxPriceThreshold": 5000,
      "MinPriceThreshold": 5000,
      "SellPricePercent": -1,
      "MaxStockThreshold": 1,
      "MinStockThreshold": 1,
      "QuantityPercent": -1
    }
  ]
}

我想要达到的是,我的列表框中列出的所有项目都应该有:

代码语言:javascript
复制
   {
  "m_Version": 9,
  "DisplayName": "Trader",
  "Icon": "Trader",
  "Color": "ef5252FF",
  "InitStockPercent": 100,
  "Items": [
    {
        "ClassName": "<listeditem of listbox>",
        "MaxPriceThreshold": 5000,
        "MinPriceThreshold": 5000,
        "SellPricePercent": -1,
        "MaxStockThreshold": 1,
        "MinStockThreshold": 1,
        "QuantityPercent": -1
    },
    {
        "ClassName": "<listeditem of listbox>",
        "MaxPriceThreshold": 5000,
        "MinPriceThreshold": 5000,
        "SellPricePercent": -1,
        "MaxStockThreshold": 1,
        "MinStockThreshold": 1,
        "QuantityPercent": -1
    }
  ]
}

XML文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<types>
    <type name="MMG_JPC_Vest_alpine">
        <nominal>1</nominal>
        <lifetime>7200</lifetime>
        <restock>7200</restock>
        <min>1</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
        <category name="clothes"/>
        <usage name="Military"/>
         <value name="Tier2"/>
        <value name="Tier2"/>
        <value name="Tier4"/>
        <usage name="Police"/>
    </type>
        <type name="MMG_MK_III_Armor_alpine">
        <nominal>0</nominal>
        <lifetime>7200</lifetime>
        <restock>7200</restock>
        <min>0</min>
        <quantmin>-1</quantmin>
        <quantmax>-1</quantmax>
        <cost>100</cost>
        <flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
        <category name="clothes"/>
        <usage name="Military"/>
        <value name="Tier2"/>
        <value name="Tier2"/>
        <value name="Tier4"/>
        <usage name="Police"/>
    </type>
  
</types>
EN

回答 1

Stack Overflow用户

发布于 2022-03-24 20:57:27

试试这段代码

代码语言:javascript
复制
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);
    XmlNode node = xmlDoc.SelectSingleNode("types");

    var rawjson = JsonConvert.SerializeXmlNode(node); 
    var json = JObject.Parse(rawjson).ToString().Replace("@","");

    var path = @"C:\";
    File.WriteAllText(Path.Combine(path, "save.json"), json);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71605453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档