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



所以我在这方面有点初学者,我遇到了一个问题
我想加载一个XML文件并从中获取一个属性,这样就可以了。现在我想把它串行化,变成一个JSON格式的文件,但我现在被卡住了,我的脑袋疯了…

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)
{
}
}
}

输出(除了一件事之外,它很好(

{
"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
}
]
}

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

{
"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文件:

<?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>

尝试此代码

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);

最新更新