快速简便地阅读 XML 并将其保存在 C# Winform 应用程序中
Quick and easy way to read through an XML and save it in a C# Winform Applicaiton
我想尝试 select 库存 Table 这样我就可以遍历每个项目 Table...但只能在属性名称为 [= 的商店下26=]...
这是我当前的代码...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Windows;
using System.Xml.Linq;
namespace Portal_of_Asura
{
public partial class ShopForm : Form
{
public string ShopName;
public string ShopKeeperName;
public string ShopSpecies;
public List<XmlNode> Items = new List<XmlNode>() { };
public ShopForm(String ShopName)
{
InitializeComponent();
}
public DataSet ds = new DataSet();
private void ShopForm_FormClosed(object sender, FormClosedEventArgs e)
{
ds.WriteXml("./ReferenceXMLS/ShopList.xml");
}
private void ShopForm_Load_1(object sender, EventArgs e)
{
ds.ReadXml("./ReferenceXMLS/ShopList.xml");
Console.WriteLine(ds.Tables["Item"].ToString());
dataGridView1.DataSource = ds.Tables["Item"];
}
}
}
这是 XML
<?xml version="1.0" encoding="utf-8" ?>
<Shops>
<Shop name="Tiarga">
<ShopKeeper name="Tim the Magnificent">
<Species>imp</Species>
</ShopKeeper>
<Stock>
<Item name="Dagger">
<Price>10 copper pieces</Price>
<Stats>10 HP</Stats>
<Quantity>1</Quantity>
</Item>
<Item name="Meal">
<Price>2 copper pieces</Price>
<Quantity>50</Quantity>
</Item>
<Item name="Iron Mace">
<Price>Free</Price>
<Quantity>1</Quantity>
<Stats>10 HP, +1 Fire</Stats>
</Item>
</Stock>
<PriceRange>Platinum Piece</PriceRange>
</Shop>
<Shop name="Tiana">
<ShopKeeper name="Tim the Magnificent">
<Species>imp</Species>
</ShopKeeper>
<Stock>
<Item name="Dagger">
<Price>10 copper pieces</Price>
<Stats>10 HP</Stats>
<Quantity>1</Quantity>
</Item>
<Item name="Meal">
<Price>2 copper pieces</Price>
<Quantity>50</Quantity>
</Item>
<Item name="Iron Mace">
<Price>Free</Price>
<Quantity>1</Quantity>
<Stats>10 HP, +1 Fire</Stats>
</Item>
</Stock>
<PriceRange>Platinum Piece</PriceRange>
</Shop>
</Shops>
到目前为止...我得到
这是每件商品...我不想要那个...只有一家商店...我确定有一些使用 linq 的方法...但我以前从未使用过 linq.. .
如果您不想反序列化,那么应该这样做:
var xml = XDocument.Load(@"C:\temp\xml.xml");
var ds = xml.Root.Descendants("Shop")
.Single(x => x.Attribute("name").Value == "Tiarga")
.Descendants("Item")
.Select(x=> new {
Name = x.Attribute("name").Value,
Price = x.Descendants("Price").FirstOrDefault()?.Value,
Stats = x.Descendants("Stats").FirstOrDefault()?.Value,
Quantity = x.Descendants("Quantity").FirstOrDefault()?.Value,
})
.ToList();
dataGridView1.DataSource = ds;
我想尝试 select 库存 Table 这样我就可以遍历每个项目 Table...但只能在属性名称为 [= 的商店下26=]...
这是我当前的代码...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Windows;
using System.Xml.Linq;
namespace Portal_of_Asura
{
public partial class ShopForm : Form
{
public string ShopName;
public string ShopKeeperName;
public string ShopSpecies;
public List<XmlNode> Items = new List<XmlNode>() { };
public ShopForm(String ShopName)
{
InitializeComponent();
}
public DataSet ds = new DataSet();
private void ShopForm_FormClosed(object sender, FormClosedEventArgs e)
{
ds.WriteXml("./ReferenceXMLS/ShopList.xml");
}
private void ShopForm_Load_1(object sender, EventArgs e)
{
ds.ReadXml("./ReferenceXMLS/ShopList.xml");
Console.WriteLine(ds.Tables["Item"].ToString());
dataGridView1.DataSource = ds.Tables["Item"];
}
}
}
这是 XML
<?xml version="1.0" encoding="utf-8" ?>
<Shops>
<Shop name="Tiarga">
<ShopKeeper name="Tim the Magnificent">
<Species>imp</Species>
</ShopKeeper>
<Stock>
<Item name="Dagger">
<Price>10 copper pieces</Price>
<Stats>10 HP</Stats>
<Quantity>1</Quantity>
</Item>
<Item name="Meal">
<Price>2 copper pieces</Price>
<Quantity>50</Quantity>
</Item>
<Item name="Iron Mace">
<Price>Free</Price>
<Quantity>1</Quantity>
<Stats>10 HP, +1 Fire</Stats>
</Item>
</Stock>
<PriceRange>Platinum Piece</PriceRange>
</Shop>
<Shop name="Tiana">
<ShopKeeper name="Tim the Magnificent">
<Species>imp</Species>
</ShopKeeper>
<Stock>
<Item name="Dagger">
<Price>10 copper pieces</Price>
<Stats>10 HP</Stats>
<Quantity>1</Quantity>
</Item>
<Item name="Meal">
<Price>2 copper pieces</Price>
<Quantity>50</Quantity>
</Item>
<Item name="Iron Mace">
<Price>Free</Price>
<Quantity>1</Quantity>
<Stats>10 HP, +1 Fire</Stats>
</Item>
</Stock>
<PriceRange>Platinum Piece</PriceRange>
</Shop>
</Shops>
到目前为止...我得到
这是每件商品...我不想要那个...只有一家商店...我确定有一些使用 linq 的方法...但我以前从未使用过 linq.. .
如果您不想反序列化,那么应该这样做:
var xml = XDocument.Load(@"C:\temp\xml.xml");
var ds = xml.Root.Descendants("Shop")
.Single(x => x.Attribute("name").Value == "Tiarga")
.Descendants("Item")
.Select(x=> new {
Name = x.Attribute("name").Value,
Price = x.Descendants("Price").FirstOrDefault()?.Value,
Stats = x.Descendants("Stats").FirstOrDefault()?.Value,
Quantity = x.Descendants("Quantity").FirstOrDefault()?.Value,
})
.ToList();
dataGridView1.DataSource = ds;